1

I am new to java and I am taking a college course right now. I was just curious what I am missing to give myself an output because right now the console is blank.

public static void main(String[] args) throws IOException {

    double avgScore;
    int highestScore, count = 0;
    String highScoringStudent;

    String names[] = null;
    int scores[] = null;

    File myFile = new File("Student_Data.txt");
    Scanner inFile = new Scanner(myFile);

    inFile.nextLine();

    while(inFile.hasNext()) {

        for(int i = 0; i > 0; i++) {
            names[i] = inFile.next();
            scores[i] = inFile.nextInt();
            count++;
        }
    }

    System.out.print("Name             Score");
    System.out.print(names[count] + "             " + scores[count]);
}
Superbia
  • 75
  • 8
  • 3
    Take a careful look at `for(int i = 0; i > 0; i++)`. What could be wrong with it? – Pshemo Nov 26 '19 at 22:07
  • 1
    After you find that problem, here are answers for your next question: [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/q/218384) – Pshemo Nov 26 '19 at 22:08

1 Answers1

2

First of all, I really wouldn't suggest to place a for inside a while loop, especially in this case (because it doesn't work). Here are the problems:

1) Your for loop starts with i = 0, and finishes right away, because !(i > 0) (i is not > 0) - so you are right, no data will be stored in the array!

2) Inside your for loop, you are reading a string, then an int one by one. After you have read them, you should move to the next string and integer to store in the next position of names and score.

3) You are not increasing i: so (if the for was working), you would just keep assigning different values to the array, at the same position (basically resetting the value of a variable every time).

This would be the best code that would work for your case:

int i = 0;

while(inFile.hasNext()) {
   names[i] = inFile.next();
   scores[i] = inFile.nextInt();
   count++;
   i++;
}

Hope this helped! :)

Enrico Cortinovis
  • 811
  • 3
  • 8
  • 31