brainfrz

Untitled

Oct 12th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Prompt {
  4.     final private static String DEFAULT_INT_PROMPT = "Please enter an integer";
  5.  
  6.  
  7.  
  8.     public static int integer( String prompt ) {
  9.         Scanner user_input = new Scanner(System.in);
  10.         int number;
  11.         boolean validInput;
  12.  
  13.         do {
  14.             System.out.print(prompt + ": ");
  15.  
  16.             try {
  17.                 number = Integer.parseInt(user_input.next());
  18.                 validInput = true;
  19.             } catch (NumberFormatException e) {
  20.                 number = 0;
  21.                 System.out.print("Input wasn't an integer. ");
  22.                 validInput = false;
  23.             }
  24.         } while (!validInput);
  25.  
  26.         return number;
  27.     }
  28.  
  29.     public static int integer() {
  30.         return integer(DEFAULT_INT_PROMPT);
  31.     }
  32.  
  33.  
  34.     public static int intGreaterThan( String prompt, int min, String error ) {
  35.         Scanner user_input = new Scanner(System.in);
  36.         int number;
  37.  
  38.         do {
  39.             if (min == Integer.MIN_VALUE) {
  40.                 System.out.print(prompt + ": ");
  41.             }
  42.             else {
  43.                 System.out.print(prompt + " (greater than " + min + "): ");
  44.             }
  45.  
  46.             try {
  47.                 number = Integer.parseInt(user_input.next());
  48.  
  49.                 if (number < min)
  50.                 {
  51.                     System.out.print(error + " ");
  52.                 }
  53.             } catch (NumberFormatException e) {
  54.                 number = min - 1;
  55.                 System.out.print("Input wasn't an integer. ");
  56.             }
  57.         } while (number < min);
  58.  
  59.         return number;
  60.     }
  61.  
  62.     public static int intGreaterThan( int min, String error ) {
  63.         return intGreaterThan(DEFAULT_INT_PROMPT, min, error);
  64.     }
  65.  
  66.     public static int intGreaterThan( int min ) {
  67.         return intGreaterThan(min, "Input out of range.");
  68.     }
  69.  
  70.     public static int wholeNumber( String error ) {
  71.         return intGreaterThan(0, error);
  72.     }
  73.    
  74.     public static int wholeNumber() {
  75.         return wholeNumber("That's a negative number.");
  76.     }
  77.  
  78.     public static int postiveInt( String error ) {
  79.         return intGreaterThan(1, error);
  80.     }
  81.    
  82.     public static int postiveInt() {
  83.         return postiveInt("That's a negative number.");
  84.     }
  85.  
  86.  
  87.     public static int intLessThan( String prompt, int max, String error ) {
  88.         Scanner user_input = new Scanner(System.in);
  89.         int number;
  90.  
  91.         do {
  92.             if (max == Integer.MAX_VALUE) {
  93.                 System.out.print(prompt + ": ");
  94.             }
  95.             else {
  96.                 System.out.print(prompt + " (less than " + max + "): ");
  97.             }
  98.  
  99.             try {
  100.                 number = Integer.parseInt(user_input.next());
  101.  
  102.                 if (number > max)
  103.                 {
  104.                     System.out.print(error + " ");
  105.                 }
  106.             } catch (NumberFormatException e) {
  107.                 number = max + 1;
  108.                 System.out.print("Input wasn't an integer. ");
  109.             }
  110.         } while (number > max);
  111.  
  112.         return number;
  113.     }
  114.  
  115.     public static int intLessThan( int max, String error ) {
  116.         return intLessThan(DEFAULT_INT_PROMPT, max, error);
  117.     }
  118.  
  119.     public static int intLessThan( int max ) {
  120.         return intLessThan(max, "Input out of range.");
  121.     }
  122.  
  123.  
  124.     public static int intInRange( String prompt, int min, int max, String minError,
  125.                                   String maxError ) {
  126.         Scanner user_input = new Scanner(System.in);
  127.         int number;
  128.  
  129.         do {
  130.             if (min == Integer.MIN_VALUE && max == Integer.MAX_VALUE) {
  131.                 number = integer(prompt);
  132.             }
  133.             else if (min == Integer.MIN_VALUE) {
  134.                 number = intLessThan(prompt, max, maxError);
  135.             }
  136.             else if (max == Integer.MAX_VALUE) {
  137.                 number = intGreaterThan(prompt, min, minError);
  138.             }
  139.             else {
  140.                 System.out.print(prompt + " (between " + min + " and " + max + "): ");
  141.             }
  142.  
  143.             try {
  144.                 number = Integer.parseInt(user_input.next());
  145.  
  146.                 if (number < min) {
  147.                     System.out.print(minError + " ");
  148.                 }
  149.                 else if (number > max) {
  150.                     System.out.print(maxError + " ");
  151.                 }
  152.             } catch (NumberFormatException e) {
  153.                 number = min - 1;
  154.                 System.out.print("Input wasn't an integer. ");
  155.             }
  156.         } while (number < min || number > max);
  157.  
  158.         return number;
  159.     }
  160.  
  161.     public static int intInRange( int min, int max, String minError, String maxError ) {
  162.         return intInRange(DEFAULT_INT_PROMPT, min, max, minError, maxError);
  163.     }
  164.  
  165.     public static int intInRange( int min, int max, String error ) {
  166.         return intInRange(min, max, error, error);
  167.     }
  168.  
  169.     public static int intInRange( int min, int max ) {
  170.         return intInRange(min, max, "Input out of range.");
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment