Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.77 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Validation {
  4.  
  5.     /** Checks if a string can be converted into a double, and converts it.
  6.     If the string can't be converted, it forces the user to enter something
  7.     that can be converted, and then converts into into a double. */
  8.     public double checkAndForceDouble (String stringToBeChecked, String messageToUser) {
  9.        
  10.         Scanner input = new Scanner(System.in);
  11.         boolean isValid = false;
  12.         double b = 0;
  13.        
  14.         try {
  15.             b = Double.parseDouble(stringToBeChecked);
  16.             isValid = true;
  17.         } catch (Exception e) {}
  18.        
  19.         while (isValid == false) {
  20.             System.out.print(messageToUser);
  21.             stringToBeChecked = input.next();
  22.             try {
  23.                 b = Double.parseDouble(stringToBeChecked);
  24.                 isValid = true;
  25.             } catch(Exception e) {}
  26.         }
  27.         return b;
  28.     }
  29.    
  30.     /** Checks if a string can be converted into a integer, and converts it.
  31.     If the string can't be converted, it forces the user to enter something
  32.     that can be converted, and then converts into into a integer. */
  33.     public int checkAndForceInt (String stringToBeChecked, String messageToUser) {
  34.        
  35.         Scanner input = new Scanner(System.in);
  36.         boolean isValid = false;
  37.         int b = 0;
  38.        
  39.         try {
  40.             b = Integer.parseInt(stringToBeChecked);
  41.             isValid = true;
  42.         } catch (Exception e) {}
  43.        
  44.         while (isValid == false) {
  45.             System.out.print(messageToUser);
  46.             stringToBeChecked = input.next();
  47.             try {
  48.                 b = Integer.parseInt(stringToBeChecked);
  49.                 isValid = true;
  50.             } catch(Exception e) {}
  51.         }
  52.         return b;
  53.     }
  54.    
  55.     /** Checks if an integer is within the specified range. If it isn't, it will force the user
  56.     to enter an integer which is within the range. */
  57.     public int checkAndForceIntBounds (int numToBeChecked, int lowerBound, int upperBound, boolean
  58.             inclusive, String errorMessage) {
  59.        
  60.         Scanner input = new Scanner(System.in);
  61.         boolean outOfBounds = true;
  62.         String tempNumHolder = "";
  63.        
  64.         if(inclusive == true) {
  65.             if(numToBeChecked>=lowerBound && numToBeChecked<=upperBound) {
  66.                 outOfBounds = false;
  67.             }
  68.            
  69.             while(outOfBounds==true) {
  70.                 System.out.print(errorMessage);
  71.                 tempNumHolder = input.next();
  72.                 numToBeChecked = checkAndForceInt(tempNumHolder, errorMessage);
  73.                 if(numToBeChecked>=lowerBound && numToBeChecked<=upperBound) {
  74.                     outOfBounds = false;
  75.                 }
  76.                                 }
  77.             }
  78.        
  79.         if(inclusive == false) {
  80.             if(numToBeChecked>=lowerBound && numToBeChecked<upperBound) {
  81.                 outOfBounds = false;
  82.             }
  83.            
  84.             while(outOfBounds==true) {
  85.                 System.out.println(errorMessage);
  86.                 tempNumHolder = input.next();
  87.                 numToBeChecked = checkAndForceInt(tempNumHolder, errorMessage);
  88.                 if(numToBeChecked>=lowerBound && numToBeChecked<upperBound) {
  89.                     outOfBounds = false;
  90.                 }
  91.                                 }
  92.                
  93.             }
  94.         return numToBeChecked;
  95.         }
  96.    
  97.     /** Checks if a double is within the specified range. If it isn't, it will force the user
  98.     to enter a double which is within the range. */
  99.     public double checkAndForceDoubleBounds(double numToBeChecked, double lowerBound, double upperBound,
  100.             boolean inclusive, String errorMessage) {
  101.     Scanner input = new Scanner(System.in);
  102.     boolean outOfBounds = true;
  103.     String tempNumHolder = "";
  104.    
  105.     if(inclusive == true) {
  106.         if(numToBeChecked>=lowerBound && numToBeChecked<=upperBound) {
  107.             outOfBounds = false;
  108.         }
  109.        
  110.         while(outOfBounds==true) {
  111.             System.out.print(errorMessage);
  112.             tempNumHolder = input.next();
  113.             numToBeChecked = checkAndForceDouble(tempNumHolder, errorMessage);
  114.             if(numToBeChecked>=lowerBound && numToBeChecked<=upperBound) {
  115.                 outOfBounds = false;
  116.             }
  117.                             }
  118.         }
  119.    
  120.     if(inclusive == false) {
  121.         if(numToBeChecked>=lowerBound && numToBeChecked<upperBound) {
  122.             outOfBounds = false;
  123.         }
  124.        
  125.         while(outOfBounds==true) {
  126.             System.out.println(errorMessage);
  127.             tempNumHolder = input.next();
  128.             numToBeChecked = checkAndForceDouble(tempNumHolder, errorMessage);
  129.             if(numToBeChecked>=lowerBound && numToBeChecked<upperBound) {
  130.                 outOfBounds = false;
  131.             }
  132.                             }
  133.            
  134.         }
  135.     return numToBeChecked;
  136.     }
  137.    
  138.    
  139.    
  140.     /** Inputs and validates an integer, with the option of checking if the integer is negative.
  141.      * Also displays a message before inputting the data
  142.      *
  143.      *  inputMessage: message displayed before inputting the data
  144.      *  dataWrongMessage: message displayed when user enters invalid data
  145.      *  checkNegative: if true, forces the user to enter a positive number
  146.      * */
  147.     public int nextIntValidated(String inputMessage, String dataWrongMessage, boolean checkNegative) {
  148.        
  149.         Scanner input = new Scanner(System.in);
  150.         int num = 0;
  151.         String rawInput = "";
  152.        
  153.         System.out.print(inputMessage);
  154.         rawInput = input.next();
  155.        
  156.         //run some checks on the input data
  157.         num = checkAndForceInt(rawInput,dataWrongMessage);
  158.        
  159.         //force the user to enter a positive value if checkNegative is true
  160.         if (checkNegative == true) {
  161.             while(num<0) {
  162.                 System.out.println(dataWrongMessage);
  163.                 rawInput = input.next();
  164.                 num = checkAndForceInt(rawInput, dataWrongMessage);
  165.             }
  166.         }
  167.         return num;
  168.     }
  169.    
  170.     /** Inputs and validates a double, with the option of checking if the double is negative.
  171.      * Also displays a message before inputting the data
  172.      *
  173.      *  inputMessage: message displayed before inputting the data
  174.      *  dataWrongMessage: message displayed when user enters invalid data
  175.      *  checkNegative: if true, forces the user to enter a positive number
  176.      * */
  177.     public double nextDoubleValidated(String inputMessage, String dataWrongMessage, boolean checkNegative) {
  178.    
  179.     Scanner input = new Scanner(System.in);
  180.     double num = 0;
  181.     String rawInput = "";
  182.    
  183.     System.out.print(inputMessage);
  184.     rawInput = input.next();
  185.    
  186.     //ensure that a double can be retrieved
  187.     num = checkAndForceDouble(rawInput,dataWrongMessage);
  188.    
  189.     //force the user to enter a positive value if checkNegative is true
  190.     if (checkNegative == true) {
  191.         while(num<0) {
  192.             System.out.println(dataWrongMessage);
  193.             rawInput = input.next();
  194.             num = checkAndForceDouble(rawInput, dataWrongMessage);
  195.         }
  196.     }
  197.     return num;
  198. }
  199.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement