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.