0

I have a getSubjects method in a Student class which is overridden in the subclasses.

I am trying to print the array using the getSubjects method.

The main class code is:

package javalab5; import java.util.*; /** * * @author Saj */ public class JavaLab5 { public static final int DEBUG = 0; /** * @param args the command line arguments */ public static void main(String[] args) { Student s[] = new Student[10]; s[0] = new MathStudent(14,15); s[1] = new MathStudent(16,19); s[2] = new MathStudent(18,21); s[3] = new MathStudent(23,28); s[4] = new ScienceStudent(32,25); s[5] = new ScienceStudent(28,56); s[6] = new ScienceStudent(29,28); s[7] = new ComputerStudent(25,38); s[8] = new ComputerStudent(34,39); s[9] = new ComputerStudent(45,56); int numberOfStudents = 0; for (int loop = 0; loop < numberOfStudents; loop++) { System.out.print("Student "+ loop + " >>"); System.out.println(s[loop].getSubjects()); } } } 

UPDATE This is the getSubjects methods:

public String getSubjects(){ return(" Science Student >> " + "Physics Grade: " + physicsGrade + " Astronomy Grade: " + astronomyGrade); } 
8
  • Does your getSubjects() method return an array? Commented Mar 14, 2017 at 18:41
  • @Surace please see the update for the getSubject method, it does not return an array. Commented Mar 14, 2017 at 18:43
  • So... what's your question, then? Commented Mar 14, 2017 at 18:43
  • @azurefrog I need it to print each of the students with their respective grades. Commented Mar 14, 2017 at 18:44
  • 3
    You are looping on 0;number of students, shouldn't you loop on s.length? Commented Mar 14, 2017 at 18:47

3 Answers 3

4

Well first of all your loop would never run because of this line: int numberOfStudents = 0; converted to english its saying this: "As long as numberOfStudents is less than 0 run this loop" but you are already at 0 and that is not less than 0! If you are trying to run through the entire array you should set it to 10 because you have 10 students:

 for (int loop = 0; loop < 10; loop++) { System.out.print("Student "+ loop + " >>"); System.out.println(s[loop].getSubjects()); } 

This should work :)

EDIT: By the way the "10" I used here is something we call a "magic number" in industry, basically a hard coded number that is not assigned to a variable, imagine if you have 10 loops with magic numbers, you would have to spend unneccesary time changing all of them if you ever needed to, this is usually the best practice to avoid magic numbers:

private final int NO_STUDENTS = 10; Students[] s = new Students[NO_STUDENTS]; //populate your array here //Now run loop for (int loop = 0; loop < s.length; loop++) { System.out.print("Student "+ loop + " >>"); System.out.println(s[loop].getSubjects()); } 
Sign up to request clarification or add additional context in comments.

1 Comment

I would suggest using s.length instead of literal 10. This way, there is no trouble when the number of students changes
1

You will never enter to your loop because you assign numberOfStudents by 0:

int numberOfStudents = 0;//<------------------------- for (int loop = 0; loop < numberOfStudents; loop++) { //-------------------------------^^------------------ 

I think you have to make s.length:

for (int loop = 0; loop < s.length; loop++) { //------------------------^------------------ 

2 Comments

OP should also throw in a null check in case he is worried that the array hasn't been filled.
mmmm, yes @BerkleyLamb and thank you, i answered just of what he do in question, so he already filled its array i'm wrong?
0

If you have 10 students, you should use:

int numberOfStudents = 10; 

But it is better to use the array's length attribute in the for loop (in that case you do not need the numberOfStudents variable):

for (int loop = 0; loop < s.length; loop++) { System.out.print("Student "+ loop + " >>"); System.out.println(s[loop].getSubjects()); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.