Advertisement
Guest User

InputHandler

a guest
Sep 11th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*
  3.  * File Name: InputHandler.java
  4.  * Package Name: N/A
  5.  * Application Name: Random String Generator
  6.  *
  7.  * Version Info: 1.0.0
  8.  * Version Date: 09-09-2014
  9.  *
  10.  * Creator: Corey D. Eacret
  11.  * Website: http://www.appdappdev.com
  12.  * Contact: appdapp@gmail.com
  13.  *
  14.  * Description:
  15.  * This is the class that handles the user interactions, and error checking. This class
  16.  * uses the Scanner class in java, and when its methods are called, it is passed arrays
  17.  * which contain the valid entries. There are two methods in this class, one for
  18.  * character input, the other for integers.
  19.  *
  20.  */
  21.  
  22. public class InputHandler {
  23.    
  24.     // This is the method for getting character input from the user.
  25.     public char inputChar(String prompt, char[] inputCheck) {
  26.        
  27.         // Just our starting variables for later.
  28.         boolean inputFlag = false;
  29.         char inputLine= 'z';
  30.        
  31.         // We now prompt the user for input; the prompt is passed to the method when it
  32.         // is called.
  33.         System.out.print(prompt + " ");
  34.         Scanner is = new Scanner(System.in);
  35.        
  36.         // This checks against a passed array for a correct entry from the user.
  37.         while (inputFlag == false) {
  38.             inputLine = is.next().charAt(0);
  39.             for (char a : inputCheck) {
  40.                 if (inputLine == a) {
  41.                     inputFlag = true;
  42.                     break;
  43.                 }
  44.             }
  45.             if (inputFlag == true){
  46.                 break;
  47.             }
  48.             else {
  49.                 System.out.print("INVALID INPUT, PLEASE TRY AGAIN:  ");
  50.             }
  51.         }
  52.        
  53.         // We send back the user input at the end.
  54.         return inputLine;
  55.     }
  56.    
  57.     // This is our second version of the method, which gets an integer from the user. It is
  58.     // identical to the above except for its variable types, so no further comments for it.
  59.     public int inputInt(String prompt) {
  60.         int inputLine = 0;
  61.         boolean inputFlag = false;
  62.         System.out.print(prompt + " ");
  63.         @SuppressWarnings("resource")
  64.         Scanner is = new Scanner(System.in);
  65.         while (inputFlag == false) {
  66.             if (is.hasNextInt()) {
  67.                 inputLine = is.nextInt();
  68.                 inputFlag = true;
  69.             }
  70.             else {
  71.                 System.out.print("INVALID INPUT, PLEASE TRY AGAIN!!\r\nPlease Enter A Valid Number:  ");
  72.                 is.next();
  73.             }
  74.         }
  75.         return inputLine;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement