Java examples for Language Basics:Console
Display an error message when the user enters incorrect input
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) ); } }