samedi 28 mars 2020

Logical Error while getting input from the user using String [duplicate]

I am required to write a Menu program that will display an error message and prompt user to enter the options again when user enter any value other than 0 to 6. It works for the first time when user enter "1" to calculate integers, but after that the calculation is done, the screens prints the output of the menu and user cannot enter any input at this time. Only allowed to enter input again for the third time.

Here are the output of the console:

Welcome to Programming Design and Implementation, Workshop 3

What would you like to do?

  1. Sum of 2 Integers
  2. Convert Temperature
  3. Convert a Character's case
  4. Print the ASCII equivalent of a Character
  5. Check if 2 Integers are Divisible
  6. Split Date into its components
  7. Exit

Please enter a value: 1 Please enter two integers 2 1 The sum of the two integers is: 3

Welcome to Programming Design and Implementation, Workshop 3

What would you like to do?

  1. Sum of 2 Integers
  2. Convert Temperature
  3. Convert a Character's case
  4. Print the ASCII equivalent of a Character
  5. Check if 2 Integers are Divisible
  6. Split Date into its components
  7. Exit

Please enter a value: Welcome to Programming Design and Implementation, Workshop 3

What would you like to do?

  1. Sum of 2 Integers
  2. Convert Temperature
  3. Convert a Character's case
  4. Print the ASCII equivalent of a Character
  5. Check if 2 Integers are Divisible
  6. Split Date into its components
  7. Exit

Please enter a value:

enter code here
String prompt = "Please enter a value: ";
String errorMsg1 = "Error: the value must be between <0> and <6>";
Scanner read = new Scanner(System.in);
String opt = "";
do // loop through the Menu until user enter zero to exit
    {
        System.out.println("Welcome to Programming Design and Implementation, Workshop 3");
        System.out.println();
        System.out.println("What would you like to do? ");
        System.out.println("> 1. Sum of 2 Integers ");
        System.out.println("> 2. Convert Temperature");
        System.out.println("> 3. Convert a Character's case");
        System.out.println("> 4. Print the ASCII equivalent of a Character");
        System.out.println("> 5. Check if 2 Integers are Divisible");
        System.out.println("> 6. Split Date into its components");
        System.out.println("> 7. Exit");
        System.out.println();
        System.out.print(prompt);
        opt = read.nextLine();

        if(opt.equals("1"))
            {
                int n1 = 0 , n2 = 0 , sum = 0;
                System.out.println("Please enter two integers");
                n1 = read.nextInt();
                n2 = read.nextInt();
                sum = n1 + n2;
                System.out.print("The sum of the two integers is: " + sum);
                System.out.println();
                System.out.println();
            } // take two integers and sum them up, after that output the answer

    } while(!opt.equals("7")); // stop the loop when option (opt) input by the user is 0
}

As seen from the console, the program will automatically print second menu screen without taking input from the user and jump straight to the third menu screen. Users are only allowed to input again in here

Aucun commentaire:

Enregistrer un commentaire