-1

I know how to print on a new line using an ArrayList but what about using a standard Array like the one shown below?

public class School {
  public static void main(String[] args) {
    Student[] studentList = new Student[10];
    //postGraduate Student #1
    myDate completionDate = new myDate(6, 10, 2019);
    Address address = new Address(199, "Hurrican road", 2568, "Australia");
    studentList[0] = new postGraduate("Greg", 6789, address, 31272890, 4565000, completionDate, "Computer Science");
    System.out.println("***Postgraduate Students***");
    for (int i = 0; i < 1; i++) {
      System.out.println(studentList[i]);
    }
  }
}

Esentinally i want the elements of each new student index("Greg", 6789, address, 31272890, 4565000, completionDate, "Computer Science") to be on a new line so it looks more tidy. Cheers

ThS
  • 4,597
  • 2
  • 15
  • 27
Andrew Van
  • 49
  • 7

4 Answers4

0

for loop condition, seems to be the issue.

Please check m.antkowicz answer.

I would suggest you to use foreach loop, to get rid of such errors

for(Student student: studentList) {
   System.out.println(student);
}

If you are using Java-8, use declarative style to enhance the readability of your code

Arrays.stream (studentList).forEach (System.out::println);

In both cases, make sure toString is overridden for Student.

NOTE: If you are using eclipse and array size is on the larger size, make sure you have sufficient console buffer How do I increase the capacity of the Eclipse output console?

Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74
0

To print on a new line try putting an empty System.out.println() line. That way, for each iteration through the loop it will go on a new line.

for (int i = 0; i < 1; i++) { System.out.print(studentList[i]); System.out.println(" "); }

nishantc1527
  • 376
  • 1
  • 12
-1

Your code is okay but you are not iterating through the whole studentList array.

If your studentList array length is 10, you want your loop to iterate from 0 to 9, not from 0 to 0 (this is what your code is doing).

To avoid hardcoding the size of studentList array, you can get its size using studentlist.length.

Hence, you can modify your loop conditions as shown below:

for (int i = 0; i < studentList.length; i++) {
      System.out.println(studentList[i]);
    }
uneq95
  • 2,158
  • 2
  • 17
  • 28
-1

in java there is a way to get length of array or array list "arrayname.length"

for (int i = 0; i < studentList.length ; i++) {
  System.out.println(studentList[i]);
}
Abnaby
  • 45
  • 8