1

When I run this, it will prompt for miles and then will get hung up and not progress to ask for mph. It is supposed to go back into the loop and if 0 is entered for miles, it will end the program. Why is it getting hung up after my while?

import java.text.NumberFormat;
import java.util.Scanner;

// use integer division to divide time into hours and minutes?

public class TravelTimeApp
{
private static double miles;
private static double milesPerHour;
private static double hours;
private static int minutes;
private static int hoursInt;

public TravelTimeApp ()
{ 
    miles = 0;
}

public static void main(String[] args)
{


    //NumberFormat
    NumberFormat onePlace = NumberFormat.getNumberInstance();
                onePlace.setMaximumFractionDigits(1);

    // create the Scanner object
    Scanner sc = new Scanner(System.in);

    System.out.printf("  Enter miles traveled, or a zero (0) to exit ->  ");
        miles = sc.nextDouble();

    if (miles == 0)
        {
            System.out.printf("\n Program completed.");
            System.exit(0);
        }
        while(miles != 0) 
        {

            System.out.printf("  Enter miles per hour rate of travel ->  ");
            milesPerHour = sc.nextDouble();


        // calculate the travel time in hours with decimal division
            hours = miles / milesPerHour;

        // get number of minutes as an int
            minutes = (int) (hours * 60);

        // use integer arithmetic to get hours and minutes as int values
            hoursInt = minutes / 60;
            minutes = minutes % 60;


        // display the result
        System.out.printf("  \nEstimated Travel Time (%,.1f miles at %,.1f mph)\n", miles,milesPerHour);
        System.out.printf("    Hours%,10d\n", hoursInt);
        System.out.printf("    Minutes%,8d\n\n", minutes);


        }

    }
 }
4
  • 1
    You never modify miles in the loop? Commented Dec 18, 2014 at 3:50
  • I need to have a while statement before the prompt asking for miles? (sorry...very new to Java) Commented Dec 18, 2014 at 3:58
  • You need to store a new value in miles at some point in the loop. Otherwise, the loop's condition will remain true forever. Commented Dec 18, 2014 at 4:03
  • @Gingerbread, please take another look at my answer. I have edited it since answering and feel it has some information you should take a look at. Commented Dec 18, 2014 at 6:03

2 Answers 2

5

Your program is not hung up. It is endlessly calculating the code in your loop because you never set the miles variable inside the loop.

To put it bluntly, because you have while(miles != 0) you need to miles = sc.nextDouble();, but I REALLY think you meant while(milesPerHour != 0) and milesPerHour = sc.nextDouble();.

Please pay attention to what each variable is for, because what you were doing was overwriting your miles variable, that you use to calculate your output. You mixed up miles and milesPerHour.

Also get rid of your if statement before the loop, it is only run once and is redundant. Although you had the right idea with System.exit(), there is no code after your loop so when the loop terminates from miles being 0, your program should exit.

Sign up to request clarification or add additional context in comments.

Comments

0

Since you are not modifying the miles inside the while loop, the loop never terminates.

You have to take miles as an input from the user again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.