Page 1 of 2 12 LastLast
Results 1 to 20 of 24
Like Tree10Likes

Thread: Loop problems

  1. #1
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default 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?

    Java 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);
        }
    
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

    Java Code:
    get input
    if(condition)
      break;
    end if

  3. #3
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    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
    JeffGrigg likes this.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.
    JeffGrigg and karla like this.

  5. #5
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    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

  6. #6
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    i still dont get it :( any more tips?

  7. #7
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    Java 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 :\
    JeffGrigg likes this.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java 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?

    Java 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.
    JeffGrigg likes this.

  9. #9
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    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

  10. #10
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default Help

    Java 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.
    Java Code:
      else 
                {
                    
               System.out.println("The overall gallon is: " );
                
                }}
    if i put (mile/gallons) i get error on MILE..anyone please?
    JeffGrigg likes this.

  11. #11
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    This does not solve your problem, but it will get you past the compile error:
    Java 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.

  12. #12
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    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 :\

  13. #13
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Here is a simple example that shows some of the main concepts:
    Java 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:
    Java 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.

  14. #14
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    This part is really important:
    Quote Originally Posted by sunde887 View Post
    Java Code:
    int x = 5;
    ???
    System.out.println(x);
    What statement(besides 'x=10;') can you use to make the 3rd line print out 10?

  15. #15
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    thanks,ill get to it with that example :) starting to understand

  16. #16
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    hey but how do you stop the loop with it?i mean,what if the user would input more than 5 mile/gallons

  17. #17
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    more like a infinite loop until some user would stop the process by entering a certain input value

  18. #18
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    Java 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?
    JeffGrigg likes this.

  19. #19
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    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?!?

  20. #20
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

Page 1 of 2 12 LastLast

Similar Threads

  1. Problems with While loop execution
    By Kevinius in forum New To Java
    Replies: 12
    Last Post: 03-03-2011, 07:35 AM
  2. Try catch loop problems :'(
    By Romally in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 08:15 PM
  3. Loop problems
    By jim01 in forum New To Java
    Replies: 3
    Last Post: 10-18-2010, 12:49 AM
  4. Newbie having problems with for loop
    By Dannii in forum New To Java
    Replies: 4
    Last Post: 04-13-2009, 11:52 PM
  5. Problems with while loop
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 07:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •