0

I am writing a program in Java and I am having trouble with a do-while loop. I have posted the code below, but I have removed most of the code for the sake of readability. What is happening when I run the program is that the first 4 lines in the do-while loop print to the screen and then I enter the "option" I choose using the keyboard and then the last line asking me if I would like to perform another action prints to the screen and then the program just ends without allowing me to type "yes or no" to indicate if I would like to perform another action. However, if I just remove the line "value = scan.nextInt();" and the line "System.out.println(value);", then when I am asked if I would like to perform an another action I am able to type "yes or no".

Does anyone understand why the do-while loop isn't working the way I have it written below?

import java.util.Scanner;

public class Test
{
   public static void main(String[] args)
   {
      String another;

      int value;

      Scanner scan = new Scanner(System.in);

      do
      {
         System.out.println("Enter the numerical value of one of the following options in order to select an option:");
            System.out.println("1. Option 1");
            System.out.println("2. Option 2");
            System.out.println("3. Option 3");

         value = scan.nextInt();
         System.out.println(value);

         System.out.print("Would you like to perform another action (yes/no)?");
         another = scan.nextLine();
      }
      while (another.equalsIgnoreCase("yes"));
   }
}

Thank you in advance.

LuCio
  • 4,285
  • 1
  • 15
  • 29
Katherine Reed
  • 291
  • 5
  • 19
  • nextInt does not possibly consume the newLine character, and then when it will read to the another variable, it reads this new line character and termines since "\n" is not equal to "yes" – Edu G Jul 30 '18 at 14:54
  • Just use `scan.next()` and this will solve the problem. – gil.fernandes Jul 30 '18 at 14:59

1 Answers1

0
import java.util.Scanner;

public class Test
{
   public static void main(String[] args)
   {
      String another;

      int value;

      Scanner scan = new Scanner(System.in);

      do
      {
         System.out.println("Enter the numerical value of one of the following options in order to select an option:");
         System.out.println("1. Option 1");
         System.out.println("2. Option 2");
         System.out.println("3. Option 3");

         value = scan.nextInt();
         System.out.println(value);
         scan = new Scanner(System.in);
         System.out.print("Would you like to perform another action (yes/no)?");
         another = scan.nextLine();
         if(another.equalsIgnoreCase("no")){
             break;
         }
      } while (true);
   }
}

Try this way

OUTPUT

Enter the numerical value of one of the following options in order to select an option:
1. Option 1
2. Option 2
3. Option 3
1
1
Would you like to perform another action (yes/no)?yes
Enter the numerical value of one of the following options in order to select an option:
1. Option 1
2. Option 2
3. Option 3
2
2
Would you like to perform another action (yes/no)?
Sumesh TG
  • 2,367
  • 1
  • 12
  • 29