berkc
berkc

Reputation: 525

Java- Using if statement inside a loop

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?

Upvotes: 1

Views: 89

Answers (4)

Vishwajit R. Shinde
Vishwajit R. Shinde

Reputation: 475

@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);

Upvotes: 0

Raj_89
Raj_89

Reputation: 721

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);

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201537

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");
    }
}
// ...

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35597

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");
     }
 }

Upvotes: 3

Related Questions