Guest User

Untitled

a guest
Jan 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1.  
  2. boolean loop = true; //a boolean
  3.  
  4. while(loop)
  5. {
  6.  
  7.     try
  8.     {
  9.  
  10.         //scan stuff in here
  11.  
  12.         loop = false;
  13.  
  14.     }
  15.     catch (java.util.InputMismatchException)
  16.     {
  17.         //if the scanner throws an error, catch it here
  18.         //make sure to keep looping!
  19.         loop = true;
  20.         System.out.println("Error Message goes here!");
  21.     }
  22.  
  23. }
  24.  
  25.  
  26. ///OR EVEN BETTER:
  27.  
  28. public static Scanner scan = null;
  29.  
  30. public static int scanInt(string ErrorMessage);
  31. {
  32.  
  33.     //this is to make sure that the scanner exists
  34.     if(scan == null);   //its either null or NULL
  35.         scan = new Scanner(System.in);  //if it does not exist, create it!
  36.  
  37.     boolean loop = true; //a boolean
  38.  
  39.     int output = 0; //output!
  40.  
  41.     while(loop) //while we need to still get input
  42.     {
  43.  
  44.         try
  45.         {
  46.  
  47.             //scan stuff in here
  48.             output = scan.nextInt();
  49.             loop = false;   //tell it not to loop
  50.    
  51.         }
  52.         catch (java.util.InputMismatchException)
  53.         {
  54.  
  55.             //if the scanner throws an error, catch it here
  56.  
  57.             //make sure to keep looping!
  58.             loop = true;
  59.  
  60.             System.out.println(ErrorMessage);
  61.  
  62.         }
  63.    
  64.     }
  65.     //return it!
  66.     return output;
  67.  
  68. }
Add Comment
Please, Sign In to add comment