### Thread: For loop count is confusing

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6

#### For loop count is confusing

I'm trying to just make a tally of the correct answers. I am 90% certain there's a simple logic error here, but I can't see what it is or how to fix it.

There are 15 possible questions, so a maximum score would be 15/15.

Code:
```for (int x = 0; x < numberOfStudents; x++) {
if (inFile.hasNext()){
question[x] = inFile.nextLine();

for (int y = 0; y < numberOfStudents; y++){
//count correct for each student
if (question[0]!= null && question[0].equals(correctAnswers[0])){
numCorrect[x][y] ++;
}
if (question[1]!= null && question[1].equals(correctAnswers[1])){
numCorrect[x][y] ++;
}
if (question[2]!= null && question[2].equals(correctAnswers[2])){
numCorrect[x][y] ++;
}
if (question[3]!= null && question[3].equals(correctAnswers[3])){
numCorrect[x][y] ++;
}
if (question[4]!= null && question[4].equals(correctAnswers[4])){
numCorrect[x][y] ++;
}
if (question[5]!= null && question[5].equals(correctAnswers[5])){
numCorrect[x][y] ++;
}
if (question[6]!= null && question[6].equals(correctAnswers[6])){
numCorrect[x][y] ++;
}
if (question[7]!= null && question[7].equals(correctAnswers[7])){
numCorrect[x][y] ++;
}
if (question[8]!= null && question[8].equals(correctAnswers[8])){
numCorrect[x][y] ++;
}
if (question[9]!= null && question[9].equals(correctAnswers[9])){
numCorrect[x][y] ++;
}
if (question[10]!= null && question[10].equals(correctAnswers[10])){
numCorrect[x][y] ++;
}
if (question[11]!= null && question[11].equals(correctAnswers[11])){
numCorrect[x][y] ++;
}
if (question[12]!= null && question[12].equals(correctAnswers[12])){
numCorrect[x][y] ++;
}
if (question[13]!= null && question[13].equals(correctAnswers[13])){
numCorrect[x][y] ++;
}
if (question[14]!= null && question[14].equals(correctAnswers[14])){
numCorrect[x][y] ++;
}
if (question[15]!= null && question[15].equals(correctAnswers[15])){
numCorrect[x][y] ++;
}```
This is my second attempt. The first had numCorrect as a one-dimensional array and no second for loop.

The issue is that, when I hard-code in a perfect score and print the value of numCorrect, it tells me 19.
I can't figure out how it is counting all the way to 19 when there are only 15 iterations.

Also, if you could give me any advice on how to make this loop work with any number of students instead of just one at a time, I would very much appreciate it.
Thank you.
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6
I realize that the second loop isn't really necessary. I was experimenting.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6
Based on another user's advice, I have shortened my if's into one if statement.

Code:
```if (response[x]!= null && response[x].equals(correctAnswers[x])){
numCorrect[x][y] ++;
}```
However, if I print numCorrect, it just says 0.
That means that the if statement isn't true even though I've made it so the answers are always correct.

My question is now: Why is my if condition not evaluating to true?
4. Why is my if condition not evaluating to true?
Try debugging the code by printing out the values of all the variables used in the if statement so you can see what the computer is seeing and perhaps then know why the if statement is never true.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6
Thank you for that suggestion.
With it, I have narrowed it down to this code bit, I think the problem is here.
Code:
```        if (inFile.hasNext()){
for (int x = 16; x>15; x--){
if (inFile.hasNext()){
response[x] = inFile.nextLine();
System.out.println(response[x]);
}
} // end for```
I am going backwards. I forgot that I did that so I could avoid the WID, but it still includes it.
I changed it back to going from 0 to 15 x++.

Now my problem is how it only increases the count to 3.
... and it also prints two 0's underneath.
The file results.txt now reads
Code:
```WID: 1234567
3
0
0```
Why is it only 3? It should be 15.
Why has it put 0 twice beneath it?
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6

#### Yes

I just figured out why it was printing 3. I had typed it correctly. Earlier, I turned off the method that creates the files and so it wasn't updating. When I removed my commenting slashes, it correctly printed 15.

I still don't know why it's printing two small numbers beneath it though.

Before it was 3, 0 , 0.
Now it's 15, 1, 1.

I can't figure out why those second ones are appearing and I don't know why they appear to linger either at 0 or 1.
7. Code:
`for (int x = 16; x>15; x--){`
Look at this code and work through how it changes x and how the loop termination test will evaluate.