The register prints an average of 63. The four scores recorded in it are 80, 75, 92 and 71, and they average 79.5. Nothing crashes and nothing warns. The program exits with status 0 and the number looks believable. A wrong answer like that is harder to catch than an error message, because you have no reason to go looking for it.
Key takeaways
new int[5]fills all five slots with 0, so a slot you never assigned is indistinguishable from a real score of zero.- Adding zeros does not change the total, which is why
total: 318stays correct whileaverage: 63is wrong. scores.lengthcounts slots, not scores, so dividing by it treats the empty slot as a student.- A
filledcounter fixes both the loop bound and the division, and(double) total / filledstops the fraction being dropped. - The array's length is fixed when
new int[5]runs, so a sixth score throwsArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5. - A checker with hardcoded expectations fails on correct code when the scores change, so it tests its input as much as your program.
The program that counts an empty desk
An array is a fixed row of numbered slots, and new int[5] builds five of them. This program stores four scores in those five slots, adds them with a for-each loop, and prints the average.
// Scores.java
public class Scores {
public static void main(String[] args) {
int[] scores = new int[5];
scores[0] = 80;
scores[1] = 75;
scores[2] = 92;
scores[3] = 71;
int total = 0;
for (int score : scores) {
total += score;
}
System.out.println("students: " + scores.length);
System.out.println("total: " + total);
System.out.println("average: " + total / scores.length);
}
}
$ javac Scores.java && java Scores
students: 5
total: 318
average: 63
The total is right, because 80, 75, 92 and 71 do add to 318. The average is wrong. The division gives 63.6, and the program prints 63 because both operands are int, so the fraction is dropped. Even 63.6 would be wrong, since the four real scores average 79.5.
Why every slot starts at zero
Creating an int array fills every slot with 0. After int[] scores = new int[5];, the statement System.out.println(scores[4]); prints 0. The program above never assigns scores[4], so the fifth slot still holds that zero.
Think of five desks in a classroom with only four students seated. The loop walks all five desks and adds whatever it finds. The empty desk adds 0, which leaves the total unchanged, so 318 survives. The damage comes from scores.length. It counts slots, not scores, so the division treats the empty desk as a fifth student who scored 0.
The loop cannot tell a stored 0 from a slot nobody touched. Both look identical, so the program has to track how many scores it actually entered.
A check you can run
Reading output by eye is how a wrong average survives. A small checker can compare it with the numbers you expect. This one reads the program's output line by line and compares each line with four expected lines.
// CheckScores.java
import java.util.List;
import java.util.Scanner;
public class CheckScores {
public static void main(String[] args) {
List<String> expected = List.of("entries: 4", "slots: 5", "total: 318", "average: 79.5");
Scanner in = new Scanner(System.in);
int passed = 0;
for (int i = 0; i < expected.size(); i++) {
String got = in.hasNextLine() ? in.nextLine() : "<nothing>";
if (got.equals(expected.get(i))) {
passed++;
System.out.println("PASS line " + (i + 1) + ": " + got);
} else {
System.out.println("FAIL line " + (i + 1) + ": expected " + expected.get(i) + ", got " + got);
}
}
if (in.hasNextLine()) {
System.out.println("FAIL extra line: " + in.nextLine());
}
System.out.println();
System.out.println(passed + " of " + expected.size() + " checks passed");
if (passed != expected.size()) {
System.exit(1);
}
}
}
$ javac CheckScores.java && java Scores | java CheckScores
FAIL line 1: expected entries: 4, got students: 5
FAIL line 2: expected slots: 5, got total: 318
FAIL line 3: expected total: 318, got average: 63
FAIL line 4: expected average: 79.5, got <nothing>
0 of 4 checks passed
The checker exits with status 1. The starter prints three lines where the checker wants four, so the fourth line fails with <nothing>. The red run also shows what the program never reports: how many scores were entered. The expected entries: 4 points straight at the missing number.
Count the scores, not the slots
The fix keeps all five slots. It adds a counter named filled for the scores actually entered, and the loop and the division both use it.
// Scores.java
public class Scores {
public static void main(String[] args) {
int[] scores = new int[5];
int filled = 0;
scores[filled++] = 80;
scores[filled++] = 75;
scores[filled++] = 92;
scores[filled++] = 71;
int total = 0;
for (int i = 0; i < filled; i++) {
total += scores[i];
}
System.out.println("entries: " + filled);
System.out.println("slots: " + scores.length);
System.out.println("total: " + total);
System.out.println("average: " + (double) total / filled);
}
}
In scores[filled++], the current value of filled picks the slot, and then filled goes up by one. After four assignments filled is 4, while scores.length stays 5. The loop stops at filled, so the empty fifth slot never enters the sum. The cast in (double) total / filled moves the division into floating point, which is why the average prints 79.5 instead of 79.
$ javac Scores.java && java Scores
entries: 4
slots: 5
total: 318
average: 79.5
$ java Scores | java CheckScores
PASS line 1: entries: 4
PASS line 2: slots: 5
PASS line 3: total: 318
PASS line 4: average: 79.5
4 of 4 checks passed
Both versions keep five slots. The only difference is which number the loop and the division use.
What the array will not do
The length of an array is fixed when new int[5] runs. No method on an array makes it longer. Store a sixth score in a five-slot array and the program fails loudly.
// Six.java
public class Six {
public static void main(String[] args) {
int[] scores = new int[5];
scores[0] = 80;
scores[1] = 75;
scores[2] = 92;
scores[3] = 71;
scores[4] = 68;
scores[5] = 88;
System.out.println("six scores stored");
}
}
$ javac Six.java && java Six
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at Six.main(Six.java:10)
This failure is the opposite of the silent average. Java stops the program and names the index and the length.
Notice also that scores.length has no parentheses. It reports the number of slots and is not a method call.
Change one score
Change the 71 to 65 in the fixed version and run the unchanged checker.
The expected values in the checker are hardcoded, so it tests the input as much as the code. With 65 in place of 71 the fixed program still prints correct arithmetic, total: 312 and average: 78.0, and the unchanged checker reports 2 of 4 checks passed because two of its four expected lines now describe a class that no longer exists. If the class gains a sixth student, you have to allocate a larger array because scores.length stays fixed at 5.