Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Validation {
- /** Checks if a string can be converted into a double, and converts it.
- If the string can't be converted, it forces the user to enter something
- that can be converted, and then converts into into a double. */
- public double checkAndForceDouble (String stringToBeChecked, String messageToUser) {
- Scanner input = new Scanner(System.in);
- boolean isValid = false;
- double b = 0;
- try {
- b = Double.parseDouble(stringToBeChecked);
- isValid = true;
- } catch (Exception e) {}
- while (isValid == false) {
- System.out.print(messageToUser);
- stringToBeChecked = input.next();
- try {
- b = Double.parseDouble(stringToBeChecked);
- isValid = true;
- } catch(Exception e) {}
- }
- return b;
- }
- /** Checks if a string can be converted into a integer, and converts it.
- If the string can't be converted, it forces the user to enter something
- that can be converted, and then converts into into a integer. */
- public int checkAndForceInt (String stringToBeChecked, String messageToUser) {
- Scanner input = new Scanner(System.in);
- boolean isValid = false;
- int b = 0;
- try {
- b = Integer.parseInt(stringToBeChecked);
- isValid = true;
- } catch (Exception e) {}
- while (isValid == false) {
- System.out.print(messageToUser);
- stringToBeChecked = input.next();
- try {
- b = Integer.parseInt(stringToBeChecked);
- isValid = true;
- } catch(Exception e) {}
- }
- return b;
- }
- /** Checks if an integer is within the specified range. If it isn't, it will force the user
- to enter an integer which is within the range. */
- public int checkAndForceIntBounds (int numToBeChecked, int lowerBound, int upperBound, boolean
- inclusive, String errorMessage) {
- Scanner input = new Scanner(System.in);
- boolean outOfBounds = true;
- String tempNumHolder = "";
- if(inclusive == true) {
- if(numToBeChecked>=lowerBound && numToBeChecked<=upperBound) {
- outOfBounds = false;
- }
- while(outOfBounds==true) {
- System.out.print(errorMessage);
- tempNumHolder = input.next();
- numToBeChecked = checkAndForceInt(tempNumHolder, errorMessage);
- if(numToBeChecked>=lowerBound && numToBeChecked<=upperBound) {
- outOfBounds = false;
- }
- }
- }
- if(inclusive == false) {
- if(numToBeChecked>=lowerBound && numToBeChecked<upperBound) {
- outOfBounds = false;
- }
- while(outOfBounds==true) {
- System.out.println(errorMessage);
- tempNumHolder = input.next();
- numToBeChecked = checkAndForceInt(tempNumHolder, errorMessage);
- if(numToBeChecked>=lowerBound && numToBeChecked<upperBound) {
- outOfBounds = false;
- }
- }
- }
- return numToBeChecked;
- }
- /** Checks if a double is within the specified range. If it isn't, it will force the user
- to enter a double which is within the range. */
- public double checkAndForceDoubleBounds(double numToBeChecked, double lowerBound, double upperBound,
- boolean inclusive, String errorMessage) {
- Scanner input = new Scanner(System.in);
- boolean outOfBounds = true;
- String tempNumHolder = "";
- if(inclusive == true) {
- if(numToBeChecked>=lowerBound && numToBeChecked<=upperBound) {
- outOfBounds = false;
- }
- while(outOfBounds==true) {
- System.out.print(errorMessage);
- tempNumHolder = input.next();
- numToBeChecked = checkAndForceDouble(tempNumHolder, errorMessage);
- if(numToBeChecked>=lowerBound && numToBeChecked<=upperBound) {
- outOfBounds = false;
- }
- }
- }
- if(inclusive == false) {
- if(numToBeChecked>=lowerBound && numToBeChecked<upperBound) {
- outOfBounds = false;
- }
- while(outOfBounds==true) {
- System.out.println(errorMessage);
- tempNumHolder = input.next();
- numToBeChecked = checkAndForceDouble(tempNumHolder, errorMessage);
- if(numToBeChecked>=lowerBound && numToBeChecked<upperBound) {
- outOfBounds = false;
- }
- }
- }
- return numToBeChecked;
- }
- /** Inputs and validates an integer, with the option of checking if the integer is negative.
- * Also displays a message before inputting the data
- *
- * inputMessage: message displayed before inputting the data
- * dataWrongMessage: message displayed when user enters invalid data
- * checkNegative: if true, forces the user to enter a positive number
- * */
- public int nextIntValidated(String inputMessage, String dataWrongMessage, boolean checkNegative) {
- Scanner input = new Scanner(System.in);
- int num = 0;
- String rawInput = "";
- System.out.print(inputMessage);
- rawInput = input.next();
- //run some checks on the input data
- num = checkAndForceInt(rawInput,dataWrongMessage);
- //force the user to enter a positive value if checkNegative is true
- if (checkNegative == true) {
- while(num<0) {
- System.out.println(dataWrongMessage);
- rawInput = input.next();
- num = checkAndForceInt(rawInput, dataWrongMessage);
- }
- }
- return num;
- }
- /** Inputs and validates a double, with the option of checking if the double is negative.
- * Also displays a message before inputting the data
- *
- * inputMessage: message displayed before inputting the data
- * dataWrongMessage: message displayed when user enters invalid data
- * checkNegative: if true, forces the user to enter a positive number
- * */
- public double nextDoubleValidated(String inputMessage, String dataWrongMessage, boolean checkNegative) {
- Scanner input = new Scanner(System.in);
- double num = 0;
- String rawInput = "";
- System.out.print(inputMessage);
- rawInput = input.next();
- //ensure that a double can be retrieved
- num = checkAndForceDouble(rawInput,dataWrongMessage);
- //force the user to enter a positive value if checkNegative is true
- if (checkNegative == true) {
- while(num<0) {
- System.out.println(dataWrongMessage);
- rawInput = input.next();
- num = checkAndForceDouble(rawInput, dataWrongMessage);
- }
- }
- return num;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement