Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 17th, 2013, 04:24 AM
 darris
Contributing User

Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
Homework - 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
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
numCorrect[x][y] ++;
}
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
March 17th, 2013, 04:45 AM
 darris
Contributing User

Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
I realize that the second loop isn't really necessary. I was experimenting.

#3
March 17th, 2013, 08:10 AM
 darris
Contributing User

Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
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
March 17th, 2013, 03:46 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,594
Time spent in forums: 2 Weeks 3 Days 3 h 41 m 44 sec
Reputation Power: 347
Quote:
 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
March 17th, 2013, 05:48 PM
 darris
Contributing User

Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
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.
Code:
```WID: 1234567
3
0
0```

Why is it only 3? It should be 15.
Why has it put 0 twice beneath it?

#6
March 17th, 2013, 05:57 PM
 darris
Contributing User

Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
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
March 17th, 2013, 05:58 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,594
Time spent in forums: 2 Weeks 3 Days 3 h 41 m 44 sec
Reputation Power: 347
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.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - For loop count is confusing