Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- * File Name: InputHandler.java
- * Package Name: N/A
- * Application Name: Random String Generator
- *
- * Version Info: 1.0.0
- * Version Date: 09-09-2014
- *
- * Creator: Corey D. Eacret
- * Website: http://www.appdappdev.com
- * Contact: appdapp@gmail.com
- *
- * Description:
- * This is the class that handles the user interactions, and error checking. This class
- * uses the Scanner class in java, and when its methods are called, it is passed arrays
- * which contain the valid entries. There are two methods in this class, one for
- * character input, the other for integers.
- *
- */
- public class InputHandler {
- // This is the method for getting character input from the user.
- public char inputChar(String prompt, char[] inputCheck) {
- // Just our starting variables for later.
- boolean inputFlag = false;
- char inputLine= 'z';
- // We now prompt the user for input; the prompt is passed to the method when it
- // is called.
- System.out.print(prompt + " ");
- Scanner is = new Scanner(System.in);
- // This checks against a passed array for a correct entry from the user.
- while (inputFlag == false) {
- inputLine = is.next().charAt(0);
- for (char a : inputCheck) {
- if (inputLine == a) {
- inputFlag = true;
- break;
- }
- }
- if (inputFlag == true){
- break;
- }
- else {
- System.out.print("INVALID INPUT, PLEASE TRY AGAIN: ");
- }
- }
- // We send back the user input at the end.
- return inputLine;
- }
- // This is our second version of the method, which gets an integer from the user. It is
- // identical to the above except for its variable types, so no further comments for it.
- public int inputInt(String prompt) {
- int inputLine = 0;
- boolean inputFlag = false;
- System.out.print(prompt + " ");
- @SuppressWarnings("resource")
- Scanner is = new Scanner(System.in);
- while (inputFlag == false) {
- if (is.hasNextInt()) {
- inputLine = is.nextInt();
- inputFlag = true;
- }
- else {
- System.out.print("INVALID INPUT, PLEASE TRY AGAIN!!\r\nPlease Enter A Valid Number: ");
- is.next();
- }
- }
- return inputLine;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement