0
public static void main(String[] args) {
    boolean run = true;
    while (run) {
        Scanner kb = new Scanner(System.in);
        System.out.println("Welcome to MetricMan - the best converter in town!");
        System.out.println("Choose and option:");
        System.out.println("A) Fahrenheit to Celsius");
        System.out.println("B) Celcius to Fahrenheit");
        System.out.println("C) Miles to Kilometers");
        System.out.println("D) Kilometers to Miles");
        System.out.println("E) Inches to Centimeters");
        System.out.println("F) Centimeters to Inches");
        System.out.println("X) Quit the program");

        System.out.print("Your choice > ");
        String choice = kb.nextLine();

        double output; int input;
        System.out.print("Enter the number of ");
        switch (choice) { // do actions according to the user's choice. convert and display
        case "A":
            System.out.print("Farenheit > ");
            input = kb.nextInt();
            output = convertFtoC(input);
            System.out.println(input+" farenheit is "+output+" celsius");
            break;
        case "B":
            System.out.print("Celsius > ");
            input = kb.nextInt();
            output = convertCtoF(input);
            System.out.println(input+" celsius is "+output+" farenheit");
            break;
        case "C":
            System.out.print("miles > ");
            input = kb.nextInt();
            output = convertMtoK(input);
            System.out.println(input+" miles is "+output+" kilometres");
            break;
        case "D":
            System.out.print("kilometres > ");
            input = kb.nextInt();
            output = convertKtoM(input);
            System.out.println(input+" kilometres is "+output+" miles");
            break;
        case "E":
            System.out.print("inches > ");
            input = kb.nextInt();
            output = convertItoC(input);
            System.out.println(input+" inches is "+output+" centimeters");
            break;
        case "F":
            System.out.print("centimeters > ");
            input = kb.nextInt();
            output = convertCtoI(input);
            System.out.println(input+" centimeters is "+output+" inches");
            break;
        case "X":
            System.out.println(); System.out.println();
            System.out.println("May the odds be ever metric!");
            run = false;
            break;
        }
        System.out.println();
        kb.nextLine();
    }
} // end of main()

there's my main method. As seen in the code, when X has been entered the code should stop running straight away, however its actual output is

...
Your choice > X
Enter the number of 

May the odds be ever metric!

there is a "enter the number of " before the actualy intended output and I see no reason for it to be there. Can anyone help fix this?

3
  • 3
    You're outputting it after you read input, but before you switch to decide what to do with it: System.out.print("Enter the number of "); Commented Jan 29, 2015 at 17:57
  • 2
    General question: Why are you creating a new Scanner object on each iteration? That's pretty awful. Commented Jan 29, 2015 at 17:58
  • If the code is supposed to stop after you input an X, you should have put a run=false;break; after the input line. Otherwise, you still have a lot of code that executes before you break out of the loop. Commented Jan 29, 2015 at 18:00

3 Answers 3

1

You are doing System.out.print("Enter the number of "); in every case .So thats why its printing when you entered X so try to put System.out.print("Enter the number of "); inside if(check if string entered is not X) like below

if(!choice.equals("X")){
System.out.print("Enter the number of ");
}

OR

put System.out.print("Enter the number of "); inside each switch case where you want it as pointed by @Tanmoy

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

Comments

1

That line occurs every time you go through the loop. One way to fix this is to make that call in each case statement except for X.

Comments

1

In your code

    System.out.print("Enter the number of ");

    switch (choice) { // do actions according to the user's choice. convert and display

so you are basically printing this before switch, so the output is expected, you may want to call inside cases rather outside switch, when its x, just quit.

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.