Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Beginner question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginner question" Watch "Beginner question" New topic
Author

Beginner question

barry richard
Greenhorn

Joined: Oct 03, 2012
Posts: 19


///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
Tina Raja
Greenhorn

Joined: Oct 05, 2012
Posts: 4
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

Joined: Oct 05, 2012
Posts: 4
In your for loop, you can not do gallons = gallons++
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10928
    
  12

This is a common error. We even have a FAQ on it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

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.


OCPJP 6 86%
barry richard
Greenhorn

Joined: Oct 03, 2012
Posts: 19
Thanks guys

That solves my problem.

Thanks for your time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginner question
 
Similar Threads
Noob java assigment
converting
Desperately need help writing Tester
Math.sqrt() 1000x slower on Solaris than Win?
Odometer