# Thread: Calculating Mileage & Breaking For Loop

1. Member Join Date
Feb 2012
Location
Posts
10
Rep Power
0

## 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;
double gallons[] = new double ;
double mpg[] = new double ;
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;
}
System.out.print("You have traveled " + count);
System.out.println(" miles");  Reply With Quote

2. Moderator  Join Date
Jul 2010
Location
California
Posts
1,638
Rep Power
12

## 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.  Reply With Quote

3. Member Join Date
Feb 2012
Location
Posts
10
Rep Power
0

## 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;
//double gallons[] = new double ;
//double mpg[] = new double ;
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;
}
}  Reply With Quote

4. Moderator  Join Date
Jul 2010
Location
California
Posts
1,638
Rep Power
12

## 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).  Reply With Quote

5. Senior Member Join Date
Nov 2011
Location
Turkey
Posts
380
Blog Entries
24
Rep Power
9

## 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;
//double gallons[] = new double ;
//double mpg[] = new double ;
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?");

{
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  Reply With Quote

6. Moderator  Join Date
Jul 2010
Location
California
Posts
1,638
Rep Power
12

## Re: Calculating Mileage & Breaking For Loop

@fatabass, recommended reading: The Problems With Spoonfeeding  Reply With Quote

#### Posting Permissions

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