# Calculating Mileage & Breaking For Loop

• 02-21-2012, 08:28 PM
dgoff267
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");
• 02-21-2012, 08:51 PM
doWhile
Re: Calculating Mileage & Breaking For Loop
Quote:

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.
• 02-21-2012, 09:17 PM
dgoff267
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;
}
}
• 02-21-2012, 10:15 PM
doWhile
Re: Calculating Mileage & Breaking For Loop
Quote:

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).
• 02-22-2012, 01:46 AM
fatabass
Re: Calculating Mileage & Breaking For Loop
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?"); 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
• 02-22-2012, 01:51 AM
doWhile
Re: Calculating Mileage & Breaking For Loop
@fatabass, recommended reading: The Problems With Spoonfeeding