Display an error message when the user enters incorrect input - Java Language Basics

Java examples for Language Basics:Console

Description

Display an error message when the user enters incorrect input

Demo Code

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
      int bank = 1000;   
      int bet;           // the bet entered by the user

      System.out.println("You can bet between 1 and " + bank);
      Scanner sc = new Scanner(System.in);
      do {/*  w w w .j a  va2  s.c  om*/
          System.out.print("Enter your bet: ");
          bet = sc.nextInt();

          if ( (bet <= 0) || (bet > bank) )
            System.out.println("wrong.");

      } while ( (bet <= 0) || (bet > bank) );
   }
}

Related Tutorials