Determining whether the input shaping methods

java.util.Scanner Import; 

/ * * 
 * @author Administrator 
 * is determined whether the input is an integer 
 * / 
public  class the Test {
     static Scanner INPUT = new new Scanner (the System. in );
     public  static  void main (String [] args) { 
        . the System OUT .println ( " Please enter an integer: " );
 //         method 1: use of type String matches method, provided the regular expression 
        String S = input.next ();
         the while (s.matches! ( " \\ + D " )) { 
           the System. OUT.println ( " not a number, please re-enter " ); 
           S = input.next (); 
         } 
         int I = Integer.valueOf (S); 
         the System. OUT .println (I); 

        
//         Method 2: Using Scanner hasNextInt method, it is determined whether the input is an integer, shaping not continue
 //         the while {(input.hasNextInt ()!)
 //             System.out.println ( "not a number, please re-enter");
 / /             input.next ();
 //         }
 //         int I = input.nextInt ();
 //         System.out.println (I); 
    } 
    
}

 

Guess you like

Origin www.cnblogs.com/Dean-0/p/11242818.html