Loop problems

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-14-2011, 12:55 PM
amzers
Loop problems
I am trying to get this program to work but the result just doesnt show...im supposed to be practicing loops for my midterm exams but i cant seem to make this work,there are no errors but when i input -1 it doesnt directly exits the program and the overall average doesnt show right...
can anyone help me or point my mistake in this one?

Code:

```    public static void main(String [] args)throws Exception {         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));                 double gallons,miles;         String gal,mil;                 do{                         System.out.println("Enter the gallons used(-1 to end): ");             gal=in.readLine();             gallons=Integer.parseInt(gal);             System.out.println("Enter the miles: ");             mil=in.readLine();             miles=Integer.parseInt(mil);             System.out.println("The miles/gallons for this tank was: " + (gallons/miles));                                 }         while (gallons !=-1);             System.out.println("The overall miles/gallons is: " + gallons/miles);     } }```
• 08-14-2011, 01:08 PM
sunde887
There is quite a bit that you need to fix. First off, I don't see where you are gathering a total. So outside of the loop it will be (-1/miles). You also don't check if the input is -1. Maybe an if to check for the -1 after getting input, and if it is -1, break out.

Code:

```get input if(condition)   break; end if```
• 08-14-2011, 01:34 PM
amzers
i will be gathering a total because it would ask me more than one then when i try to exit the program using (-1) i would be able to display the TOTAL...for example:

{
Enter the gallons used (-1 to end): 12.8
Enter the miles driven: 287
The miles/gallons for this tank was: 22.421875

Enter the gallons used (-1 to end): 10.3
Enter the miles driven: 200
The miles/gallons for this tank was: 19.417475

Enter the gallons used (-1 to end): -1

The overall average miles/gallon was: 21.601423
}

i dont get it if i had to use IF statements :\ lol im really confused bout this
• 08-14-2011, 01:40 PM
sunde887
Well think about what you want it to do, and what it does. Look at the code in each line and figure out what is happening at each step. What happens when the user enters -1? does it have anywhere in the code which forces it to exit the loop? You also aren't keeping a total of of the input being put in as you are going. If you don't the final result will just give you the last entered data. You need to carefully design this to get it working correctly. Try writing out what you want this to do in plain english at each step through the code.
• 08-14-2011, 01:42 PM
amzers
yea ill try..lemme work on that :) i hope id get this by tonight and ill reply soon as i get better results or make this work
• 08-14-2011, 04:37 PM
amzers
i still dont get it :( any more tips?
• 08-14-2011, 04:47 PM
amzers
Code:

```    public static void main(String[]args)throws Exception{         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));             double nm1,nm2;         String num1,num2;                 do{                         System.out.println("Enter 1st number: ");             num1=in.readLine();             nm1=Integer.parseInt(num1);             if (nm1==-1)                 System.out.println("Exits Program: ");             else                         System.out.println("Enter 2nd number: ");             num2=in.readLine();             nm2=Integer.parseInt(num2);             System.out.println("total is: " +(nm1+nm2));                                                 }         while(nm1 != -1);         System.out.println("Exits: " );     }}```
i made this dummy and somehow i still cant get the overall sum of nm1 and nm2 :\
• 08-14-2011, 07:48 PM
sunde887
You should really be using more meaningful names. Perhaps totalGas, totalMileage, gas, and mileage. Better names make easier to read code.

Ok, so first things first; you get user input. What do you want to do with said input? Is there a chance that anything special should happen based on the input? Or should it just use the input for calculations no matter what? What happens if they use the sentinel as input? What does the statement
Code:

`System.out.println("Exits Program: ");`
do? Does it exit? Why not? Do you want it to exit the program? Why?

How can you get out of a loop? Are there any keywords that sound like perhaps that may be what they do?

After getting the inputs and not receiving the sentinel(-1), what should you do with the inputs? Should you increase the total? How can you add some amount to a variable?

Code:

```int x = 5; ??? System.out.println(x);```
What statement(besides 'x=10;') can you use to make the 3rd line print out 10?

Use this to add to the totalGas and totalMileage with each pass through the loop. This is just some food for thought. You need to really be thinking about all the problems that can occur, and what you want to code to do. All details/situations must be handled. The computer does what you say to do; not what you think/want it to do. The more exact the instructions, the more it will behave how you want.
• 08-15-2011, 08:57 AM
amzers
what i am trying to do here is get the total of everything after i exit/input the sentinel loop...i cant seem to get it,and ive been working with this for the whole night...

i know this is just simple for you guys but im still new to java :\
any more simpler thoughts on this one
• 08-15-2011, 04:42 PM
amzers
Help
Code:

```    public static void main(String [] args)throws Exception {         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));                 double gallons,mile,mpg;         String gal,mil;                               do{                                         System.out.println("Enter the gallons used(-1 to end): ");             gal=in.readLine();             gallons=Integer.parseInt(gal);                         if (gallons!=-1)             {                  System.out.println("Enter the miles: ");             mil=in.readLine();             mile=Integer.parseInt(mil);             System.out.println("The miles/gallons for this tank was: "  + (mile/gallons));                     }                         else             {                           System.out.println("The overall gallon is: " );                         }}             while(gallons!=1);                                             }}```
all i need now is to print the combined miles per gallon obtained for all tankfuls.
Code:

```  else             {                           System.out.println("The overall gallon is: " );                         }}```
if i put (mile/gallons) i get error on MILE..anyone please?
• 08-15-2011, 05:31 PM
JeffGrigg
This does not solve your problem, but it will get you past the compile error:
Code:

`double gallons, mile = 0, mpg;`
sunde887 gave several really useful and insightful hints above. You might want to read them again, and think about them.
• 08-15-2011, 05:43 PM
amzers
its not that i didnt read what sunde wrote but its just that i didnt understand what he meant about everything...
sigh all i want to do is understand how to total everything up and loops by not making it too much complicated :\
• 08-15-2011, 06:00 PM
JeffGrigg
Here is a simple example that shows some of the main concepts:
Code:

```public class Rocks {         public static void main(String[] args) {                 int totalWeightOfAllRocks = 0;                 for (int weightOfThisRock : new int[] {6,4,3,8,3,7,4,5}) {                         System.out.println("This rock weighs " + weightOfThisRock + " lbs.");                         totalWeightOfAllRocks = totalWeightOfAllRocks + weightOfThisRock;                 }                 System.out.println("We had, in total " + totalWeightOfAllRocks + " pounds of rocks.");         } }```
Here's the output:
Code:

```This rock weighs 6 lbs. This rock weighs 4 lbs. This rock weighs 3 lbs. This rock weighs 8 lbs. This rock weighs 3 lbs. This rock weighs 7 lbs. This rock weighs 4 lbs. This rock weighs 5 lbs. We had, in total 40 pounds of rocks.```
• 08-15-2011, 06:02 PM
JeffGrigg
This part is really important:
Quote:

Originally Posted by sunde887
Code:

```int x = 5; ??? System.out.println(x);```
What statement(besides 'x=10;') can you use to make the 3rd line print out 10?

• 08-15-2011, 06:03 PM
amzers
thanks,ill get to it with that example :) starting to understand
• 08-15-2011, 06:06 PM
amzers
hey but how do you stop the loop with it?i mean,what if the user would input more than 5 mile/gallons
• 08-15-2011, 06:07 PM
amzers
more like a infinite loop until some user would stop the process by entering a certain input value
• 08-15-2011, 06:27 PM
amzers
Code:

``` */import java.io.*; public class lola {     public static void main(String[]args)throws Exception{         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));         int lo,la=0,bot;         String o,a,b;                 do{             System.out.println("Enter LO: ");             o=in.readLine();             lo=Integer.parseInt(o);             if (lo!=-1)             {             System.out.println("Enter Bot: ");             b=in.readLine();             bot=Integer.parseInt(b);             la=la+bot+lo;             }             else             {             System.out.println("lalala" + (la));                                 }}         while(lo!=-1);         System.out.println("ambot " + (la));     }     }```
i know the coding is messed up and the data type names are stupid but i managed to make it work somehow...any words if i done this right?
• 08-15-2011, 09:35 PM
JeffGrigg
If it consistently produces the results you want for all reasonable cases, then it is "correct."

It does not, of course, produce "gallons per mile" reports. But hey; what were you driving, a tank?!? :(devil):
• 08-16-2011, 04:25 AM
tnrh1
Why do you use BufferedReader and not Scanner?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last