1

I wrote a small code :

System.out.println("Please enter a number");
int number = scan.nextInt();

if(number == 1) {
    System.out.println("Number is 1");
} else if(number == 2) {
    System.out.println("Number is 2");
} else {
    System.out.println("Invalid selection");
}

When the user enters a number different than 1 and 2, user gets "Invalid selection" message and then code terminates. I don't want it to terminate, I want it to run again until user writes 1 or 2. I tried do-while loop but it become an infinite loop. What are your suggestions?

1
  • 2
    I tried do-while loop but it become an infinite loop. Show us. Commented Nov 5, 2014 at 7:23

4 Answers 4

3

You can use while loop here

 Scanner scanner = new Scanner(System.in);
 boolean status = true;
 while (status) { // this runs if status is true
    System.out.println("Please enter a number");
     int number = scanner.nextInt();
     if (number == 1) {
        System.out.println("Number is 1");
        status=false; // when your condition match stop the loop
     } else if (number == 2) {
        System.out.println("Number is 2");
        status=false;// when your condition match stop the loop
     } else{
        System.out.println("Invalid selection");
     }
 }
Sign up to request clarification or add additional context in comments.

Comments

2

Try this...

int number;
do{
    System.out.println("Please enter a number");
    number = scan.nextInt();
  if(number == 1)
    {
      System.out.println("Number is 1") ;
    }

  else if(number == 2)
   {
      System.out.println("Number is 2") ;
    }
  else
   {
      System.out.println("Invalid selection") ;
    }
 }while(number!=1 && number!=2);

Comments

1

I recommend you check if there is an int with Scanner.hasNextInt() before you call Scanner.nextInt(). And, that makes a nice loop test condition if you use it in a while loop.

Scanner scan = new Scanner(System.in);
System.out.println("Please enter a number");
while (scan.hasNextInt()) {
    int number = scan.nextInt();
    if (number == 1) {
        System.out.println("Number is 1");
        break;
    } else if (number == 2) {
        System.out.println("Number is 2");
        break;
    } else {
        System.out.println("Invalid selection");
    }
}
// ...

Comments

0

@Dosher, reposting @Raj_89's answer with correction in while loop condition. Please notice While loop condition

int number = 0;
        do{
            System.out.println("Please enter a number");
            Scanner scan = new Scanner(System.in);
            number = scan.nextInt();
          if(number == 1)
            {
              System.out.println("Number is 1") ;
            }

          else if(number == 2)
           {
              System.out.println("Number is 2") ;
            }
          else
           {
              System.out.println("Invalid selection") ;
            }
         }while(number==1 || number==2);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.