• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Beginner question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


///OUTPUT IS///
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters

We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters

We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters

We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters

We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters

We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters
We have 3.75 liters

.....and so on.
Problem 1: No calculations are being done
Problem 2: Loop does not end at 20

I am running this on debian squeeze,

java version is


Thanks
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am quite new at JAVA however, I tried what you posted and it works for me - trying using this code

class Example{
public static void main(String [] args){
double gallons;
double liters;
int counter;

counter = 0;

for (gallons = 1; gallons <=20; gallons++){
liters = gallons * 3.75;
System.out.println("We have " + liters + " liters");
counter++;
if (counter == 5){
System.out.println();
counter = 0;
}

}
}
}
 
Tina Raja
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your for loop, you can not do gallons = gallons++
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a common error. We even have a FAQ on it.
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tina Raja wrote:I am quite new at JAVA however, I tried what you posted and it works for me - trying using this code

class Example{
public static void main(String [] args){
double gallons;
double liters;
int counter;

counter = 0;

for (gallons = 1; gallons <=20; gallons++){
liters = gallons * 3.75;
System.out.println("We have " + liters + " liters");
counter++;
if (counter == 5){
System.out.println();
counter = 0;
}

}
}
}



Tina, please edit your post and insert code tags properly.
 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys

That solves my problem.

Thanks for your time
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic