/* John Bonifas ACO 210 Spring 2013 Project 3 */ package project3; import java.util.Scanner; /** Encapsulates data input functionality for this project. Reads from the console input stream, and attempts to validate the data read in. If the data is in the correct format, readInput() will return an object containing a flag indicating a successful data read, and the data read. If not, readInput() will return a flag indicating failure and an empty dataset. @author Slowly */ public class readInput { /** Constructs a readInput object. */ public readInput() {} /** Gets data from an input stream and, if the data is in the correct format, returns an outputPackage object with the data and a data read success indicator. */ public outputPackage readInput() { //setup String strIn; String[] strCleanup; outputPackage objPackage = new outputPackage(); //get the raw data Scanner objIn = new Scanner(System.in); System.out.print("\nEnter Memory size and frame size, separated by a comma: "); strIn = objIn.nextLine().toLowerCase(); //clean it up /* This was a toughie; I had to come up with a regex that worked in java but did not include numbers the first time I used this for a project. Now I use the templates below. REGEXES ======= "^[A-Za-z_ ]+$" text, include underscore no numbers "[0-9]" numeric digits only "^(?=.*\d)[\d\s]{3}$" 2 digits no more no less "^\\d+[\\,]\\d+$" any integer, a comma, then any integer */ if(strIn.matches("^\\d+[\\,]\\d+$")) { // data is good strCleanup = strIn.split(","); objPackage.setData(strCleanup); return objPackage; } else { // data is bad objPackage.setSuccess(0); return objPackage; } } /** Static utility that checks to see if the given input string is an Integer. @param inNum @return is or is not an Integer */ public static boolean checkIfNumber(String inNum) { try { Integer.parseInt(inNum); } catch (NumberFormatException ex) { return false; } return true; } }