Code:
/*
1 meter is 39.37 inches.
This program outputs a table of twelve feet's worth of inch-to-meter
conversions, and outputs a blank line every 12 inches.
*/
class InchtoMeter {
public static void main(String[] args) {
int inches, counter;
for (inches = 0; inches <= 144; inches++) {
counter++;
if (counter == 12) {
System.out.println(inches/12+" feet is " +
inches/39.37D + " meters.");
System.out.println();
counter = 0;
}
else {
System.out.println(inches+" inches is " +
inches/39.37D + " meters.");
}
}
}
}
and this is the error I get from Sun JDK:
Quote:
InchtoMeter.java:10: variable counter might not have been initialized
counter++;
^
1 error
|
"Might not have been initialized"??? WTF?
I solved the problem by realizing the difference between "declared" and initialized, and assigned counter the value of zero outside the scope of for. I also initialized inches to 1 instead of to 0, because it
was saying every 11 inches was a foot!