import java.util.Scanner;
public class InputLoop
{
public static void main(String[] args)
{
System.out.println("Enter an integer to continue or a non-integer to finish");
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
while(scan.hasNextInt())
{
System.out.println("Enter an integer to continue or a non-integer to finish");
scan.next();
}
}
}
This is the first while loop i’ve written, and when it runs, I have to input a number twice before it proceeds to print “Enter an integer to continue…..”
Thanks for any help!
This would be much better as a do …while loop. Essentially, it is the same, except you check the condition after you are done.
Although if you did it as a while, you could add in a dummy variable to make sure you go through at least once.