Help with java code.

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
My java program won't compile and as usual the compiler gives a useless error message.

Code:
class galltolit
{
public static void main(String[] args)
{
double gallons;
double litres;
gallons = 10;
litres = gallons*4.6;
System.out.println(gallons+" gallons is "+litres+" litres ");
for(litres;litres<50;litres++)
System.out.println(litres);
System.out.println("done");
}}

Code:
galltolit.java:10: not a statement
for(litres;litres<50;litres++)
    ^
1 error
 
Last edited:
Hi,

Try setting the initialization of the loop to be:

for (litres=litres;litres<50;litres++)

It would probably be better coding though to have another variable for your loop though so the loop leaves the original litres value as it was when the loop was entered.

Hope that helps.
Jim
 
JIMA said:
Hi,

Try setting the initialization of the loop to be:

for (litres=litres;litres<50;litres++)

It would probably be better coding though to have another variable for your loop though so the loop leaves the original litres value as it was when the loop was entered.

Hope that helps.
Jim


Thanks. :)
 
You could always just skip the initialisation of the index variable...

Code:
for (; litres < 50; litres++) {
...
}


EDIT...

Is the for loop there just to print out the numbers 46 - 49?

If so, I would do something like this:

Code:
for (int count = litres; count < 50; count++) {
   System.out.println(count);
}

As JIMA said, the value of litres will remain unchanged.
 
Last edited:
Back
Top Bottom