0

I am having some trouble getting my while loop to reiterate when executed. The if statements work as expected but the char value returned from the scanner isn't getting to my while loop. Anyone know what I did wrong or a better solution to my program?

package classWork;
import java.util.Scanner;

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

        Scanner input = new Scanner(System.in);
        int choice = 0;
        double celsius, fahrenheit, inches, centimeters;
        char doAgain = 'y';

        while (doAgain == 'y' || doAgain == 'Y')
        {

        System.out.print(" Main Menu \n 1. Celsius to Fahrenheit \n 2. Inches to Centimeters \n"
                + "Please select either 1 or 2 \nThen press enter to continue");
        choice = input.nextInt();

        if (choice == 1)
        {
            System.out.println("This program will convert a celsius value into a fahrenheit one. \n");

            System.out.println("Please enter the tempature for conversion: ");
            celsius = input.nextInt();

            fahrenheit = 1.8 * celsius + 32.0;

            System.out.println(celsius + " degree(s) in celcius" + " is " + fahrenheit + 
                    " in fahrenheit. \n");

            System.out.println("Do you want to continue?\n: " +
                       "enter a \'y \' for another round\n " +
                      " enter a\'n\'   to end the program\n");

            doAgain = input.next().charAt(0);

            input.close();
            return;
        }

        if (choice == 2)
        {
            System.out.println("This program will convert inches to centimeters. \n");


            System.out.println("Please enter the length for conversion: ");
            inches = input.nextInt();

            centimeters = 2.54 * inches;

            System.out.println(inches + " inches is " + centimeters + " centimeters. \n");

            System.out.println("Do you want to continue?\n: " +
                       "enter a \'y \' for another round\n " +
                      " enter a\'n\'   to end the program\n");

            doAgain = input.next().charAt(0);

            input.close();
            return;
        }

        else
        {
            System.out.println("That is not a valid option. Please restart and try again");
            input.close();
            }
        }
    }
}
3
  • 3
    You have a return after asking the user if they want to continue. That will exit the method (and the loop, obviously). Commented Sep 28, 2016 at 18:30
  • If you want to repeat you should not have a return at the end of your if statement blocks Commented Sep 28, 2016 at 18:31
  • Now I feel silly because the answer was rather obvious, but hey sometimes that's the way it goes. Thanks a bunch to all who answered it really did help! Commented Sep 28, 2016 at 22:23

2 Answers 2

1

There are actually 3 issues in your program

1. Don't call return to keep iterating

At the end of the 2 if blocks you call return which makes your application exit from the main method so exit from your application that is actually your current issue.

So simply remove them or use continue instead.

2. Don't call close to keep reading

At the end of the 2 if blocks you call input.close() which closes the scanner so you won't be able to read anything later without getting a IllegalStateException.

So simply remove them.

3. The second if should be an else if

your code should be:

if (choice == 1)
{
    ...
}
else if (choice == 2)
{
    ...
}
else
{
    ...
}

Because otherwise if your first choice is 1 then you type y to iterate your application will go inside the else since choice != 2 which will close the scanner as it is wrongly considered as a non valid option.

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

1 Comment

Thank you, that was spot on and now so is my program!
1

You need to remove the return , it finishes the execution of your main thread

Comments

Your Answer

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