0

When I run this code, for some reason when it hits test10 to be added into the Array sort, the addListing method ignores the for loop and just skips to the bottom. I am curious why the for loop runs for test2.addListing(test); and test2.addListing(test9); but not for the one after.

import java.util.Scanner;


public class TestListings {

public static void main(String[] args) {

StudentListings test = new StudentListings();
StudentListings test9 = new StudentListings();
StudentListings test10 = new StudentListings();
test.input();
test9.input();
test10.input();
Scanner sc = new Scanner(System.in);
int aSize = 0;
System.out.print("Enter Array Size: ");

aSize = Integer.parseInt(sc.nextLine());
ArraySort test2 = new ArraySort(aSize);
test2.addListing(test);
test2.addListing(test9);
test2.addListing(test10);

test2.showAllListings();



}
}

This is the method written, and it runs for the first run through, next = 0; intially, but the 3rd time (in test10) it just looks at the line and skips it.

public class ArraySort

{

  private StudentListings[] data;

  private int size = 0;

private int next = 0;
public ArraySort() 
{
  data = new StudentListings[size];
  size = 0;
  next = 0;

}

public ArraySort(int ArraySize)
{
  size = ArraySize;
  data = new StudentListings[size];
  next = 0;
}


public void addListing(StudentListings newListing)
{
  System.out.print(next);

  for(i = next - 1; i <= 0; i--)
  {


     try { 
     if (newListing.compareTo(data[i].getLName()) < 0)
     {
        data[i+1] = data[i].deepCopy();
     }
     else
     {
        data[i+1] = newListing;
       next++;
        break;
     }
     }
     catch(ArrayIndexOutOfBoundsException | NullPointerException exception) 
    {
     int x = i + 1;
     data[x] = newListing;
     next++;
     break;
     }

    }      
    System.out.print(next);

  } 

   public void showAllListings()
 {
     for(int i = 0; i < next; i++)
     {
     System.out.println((i + 1) + ". " + data[i]);
     }
  }
} 

This is the class that is getting created to be inserted into the array.

    import java.util.Scanner;

   public class StudentListings {

    private String firstName;
    private String lastName;
    private int id;
    private double gpa;

   public StudentListings() 
   {
    firstName = "";
    lastName = "";
    id = 0;
    gpa = 0.0;

    }

    public StudentListings(String firstName, String lastName, int id, 
    double gpa) 
    {

     this.firstName = firstName;
     this.lastName = lastName;
     this.id = id;
     this.gpa = gpa;
     }  

     public void setName(String firstName, String lastName) 
     {

      this.firstName = firstName;
      this.lastName = lastName;
      }

     public String getName() 
     {

     return firstName + " " + lastName;

     }

     public void setId(int id) 
     {

     this.id = id;
      }

     public int getId() 
     {

     return id;
     }


     public void setGpa(double gpa) 
     {

     this.gpa = gpa;
     }

     public double getGpa() 
     {

      return gpa;
     }

     public String getLName()
    {

     return lastName;
     }

    public void input()
     {

    Scanner sc = new Scanner(System.in);

    System.out.print("Enter First Name: ");

    this.firstName = sc.nextLine();

    System.out.print("Enter Last Name: ");

    this.lastName = sc.nextLine();

    System.out.print("Enter Student ID: ");

    this.id = sc.nextInt();

    System.out.print("Enter Student GPA: ");

    this.gpa = Double.parseDouble(sc.next());

    }

    public String toString()
    {

    return "Last Name: " + lastName + " First Name: " + firstName + " ID: 
    " + id + " GPA: " + gpa;
    }

    public StudentListings deepCopy() 
    {
    StudentListings clone = new StudentListings(firstName, lastName, id, 
    gpa);
    return clone;
    }

    public int compareTo(String targetKey) 
    {
    return(lastName.compareTo(targetKey));
    }

    }
Hudson2D
  • 1
  • 1
  • what is the value of next at that time? – Stultuske Sep 27 '18 at 05:48
  • From where are you getting `next`? You are neither passing it nor initialising it!! I think it's the incomplete snippet, please share the full code, so we can clone the conditions in our system. – Abhishek Keshri Sep 27 '18 at 05:50
  • addListing is part of a class, with a constructor that intializes next as 0; I had tested the code, where it outputted to the console what next would = after it time it runs and before it ran, when the third time it ran, it equaled 2. – Hudson2D Sep 27 '18 at 05:55
  • @Hudson2D see my edited comment!! – Abhishek Keshri Sep 27 '18 at 05:56
  • @AbhishekKeshri updated to add all the code, I have for the project. I left in the part where I print out the value of next to keep track of it. – Hudson2D Sep 27 '18 at 06:10

2 Answers2

0

If next is 0 the first time then it’s 2 the third time and i starts at 1 so the condition i <= 0 is false from the start

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Wouldn't when it be if i =2 on the third time around, shouldn't it run twice before i would be less then or equal to 0. Or am I misunderstanding how the for loop should work. – Hudson2D Sep 27 '18 at 06:11
  • seeing as you have to start your answer with "if", I would recommend posting this as a comment, rather than an answer – Stultuske Sep 27 '18 at 06:13
  • @Hudson2D i is initalized to next -1 so it's 1 and also the end condition for the loop is `i <= 0` so for any value larger than 0 the loop will never enter since this condition is false from the start. – Joakim Danielson Sep 27 '18 at 06:55
  • my point is, you didn't know whether or not that was the case, making it a comment (guess) rather than an answer – Stultuske Sep 27 '18 at 07:06
  • it said so in the question, after the edit, which appeared after your answer – Stultuske Sep 27 '18 at 07:16
  • I'm merely saying I did a refresh of the page, there was no update of the question yet and it didn't mention a value of n, but it did show your answer, which is why I commented what I did – Stultuske Sep 27 '18 at 07:33
  • @Stultuske I have deleted my previous comments because I don't have to prove anything to you, I know what I read and I used that information in my answer. – Joakim Danielson Sep 27 '18 at 07:57
0

I'm not solving that problem, because in my opinion you're trying to do (intricately) something already defined in Java. When you create a class, and have to manage an array of object of that class, Java offers a very simple way to do that, I'll explain what I would do in your position step by step:

1 - The first thing to do is to define the comparison between the object belonging to that class, you can achieve that by overriding the method compareTo of that class (the class has to implement Comparable <YourObject>); in your case i guess it schould be something like:

public class StudentListings implements Comparable<StudentListings>{
...
@Override
public int compareTo(StudentListings element){
    return ...;
    }
}

In which you define when a StudentListing object is bigger than another.

2 - The second thing to do is to define an ArrayList<StudentListings> in your main, and initialize it:

ArrayList<StudentListings> yourArray = new ArrayList<>();

3 - Then you have to add the elements to that array (obviously after you initialized them):

yourArray.add(test);
yourArray.add(test9);
yourArray.add(test10);

4 - Now you have your array, not sorted, to sort it you just have to call the method

Collections.sort(yourArray);

Now you have your ArrayList of StudentListings sorted.

There is another way to achieve this result, that is described here, I don't like it very much because using that way you have to redefine the comparison everytime you need to sort an array and because your main code results more complex, but it has the same result of the steps I explained (therefore the linked method is useful if you have to sort two different arrays of the same class objects in different ways, eg. one by students name and the other by students surname).

Ruka
  • 1
  • 2