Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By doWhile

Thread: Calculating Mileage & Breaking For Loop

  1. #1
    dgoff267 is offline Member
    Join Date
    Feb 2012
    Location
    Philadelphia
    Posts
    10
    Rep Power
    0

    Default Calculating Mileage & Breaking For Loop

    Hey all,

    I'm trying to create an application that calculates total mileage traveled and subtracts the initial mileage reading on the odometer. Can someone please tell me what I'm doing wrong? Also, can you tell me how I can break the For loop by inputting a negative number or typing in "stop"? I pasted the code below. Thanks for your help!

    import java.util.Scanner;

    public class Lab4{
    public static void main(String args[]){
    int miles[] = new int[10];
    double gallons[] = new double [10];
    double mpg[] = new double [10];
    Scanner kb = new Scanner(System.in);
    int count=0;
    double counter=0.0;
    for (int i = 0; i < 1; i++){
    System.out.println("What is the reading on the odometer?");
    miles = kb.nextInt();
    if (i > 0)
    count = count + miles;
    }
    for (int i = 1; i < 9; i++){
    System.out.println("How many miles have you traveled since last filling up?");
    miles = kb.nextInt();
    if (i > 0);
    count = count + miles - miles[0];
    }
    System.out.print("You have traveled " + count);
    System.out.println(" miles");

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Calculating Mileage & Breaking For Loop

    Can someone please tell me what I'm doing wrong?
    You tell us...does this compile? Are there exceptions? Does it misbehave? Provide context to your problem and your chances of receiving help in a reasonable amount of time skyrocket.

  3. #3
    dgoff267 is offline Member
    Join Date
    Feb 2012
    Location
    Philadelphia
    Posts
    10
    Rep Power
    0

    Default Re: Calculating Mileage & Breaking For Loop

    Sorry about that. When I run the program, I don't get any errors or exceptions. I tried formatting it a different way, and I think I have it working much better now(I pasted the new code below). What can I do so that the user can cut out of the loop early, instead of always having to type in a number for the mileage 10 times? Can I create something that says "if the user inputs a negative number or a certain word, then the loop will stop"?

    import java.util.Scanner;

    public class Lab4{
    public static void main(String args[]){

    int miles[] = new int[10];
    //double gallons[] = new double [10];
    //double mpg[] = new double [10];
    Scanner kb = new Scanner(System.in);
    int count=0;

    for (int i = 0; i < 10; i++){
    System.out.println("How many miles have you traveled since last filling up?");
    miles[i] = kb.nextInt();
    if (i > 0);
    count = count + miles [i];
    }
    System.out.print("You have traveled " + count);
    System.out.println(" miles");


    //for (double j = 0; j < 10; j++){
    //System.out.println("How many gallons did you fill up?");
    //gallons[j] = kb.nextDouble();
    //if (j > 0.0)
    //counter = counter + gallons;
    }
    }

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Calculating Mileage & Breaking For Loop

    Can I create something that says "if the user inputs a negative number or a certain word, then the loop will stop"?
    Why not? Just evaluate what the user entered and if its negative then break out of the loop (and indicate to the user this through the println statement).

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Calculating Mileage & Breaking For Loop

    Java Code:
    import java.util.Scanner;
    
    public class Lab4{
    public static void main(String args[]){
    
    int miles[] = new int[10];
    //double gallons[] = new double [10];
    //double mpg[] = new double [10];
    Scanner kb = new Scanner(System.in);
    int count=0;
    
    for (int i = 0; i < 10; i++){
    System.out.println("How many miles have you traveled since last filling up?");
    
    int addMiles = kb.nextInt();
    
    if(addMiles > 0)
    {
    	miles[i] = addMiles;
    	count = count + miles [i];
    }
    else
    {
    	i = 9;
    }
    
    }
    System.out.print("You have traveled " + count);
    System.out.println(" miles");
    
    
    //for (double j = 0; j < 10; j++){
    //System.out.println("How many gallons did you fill up?");
    //gallons[j] = kb.nextDouble();
    //if (j > 0.0)
    //counter = counter + gallons;
    }	
    }
    Output:
    How many miles have you traveled since last filling up?
    43
    How many miles have you traveled since last filling up?
    4
    How many miles have you traveled since last filling up?
    -1
    You have traveled 47 miles

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Calculating Mileage & Breaking For Loop

    @fatabass, recommended reading: The Problems With Spoonfeeding
    DarrylBurke likes this.

Similar Threads

  1. Breaking out a timer loop
    By musico in forum New To Java
    Replies: 6
    Last Post: 06-23-2011, 08:29 PM
  2. Breaking the thread.
    By JDaniel in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-21-2010, 03:22 PM
  3. breaking up
    By smray7 in forum New To Java
    Replies: 2
    Last Post: 10-31-2010, 07:58 AM
  4. breaking out of while loop
    By mac in forum New To Java
    Replies: 5
    Last Post: 05-18-2010, 03:21 PM
  5. Breaking up of array
    By agarwal_srushti in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 07:03 PM

Posting Permissions

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