Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Prompt {
- final private static String DEFAULT_INT_PROMPT = "Please enter an integer";
- public static int integer( String prompt ) {
- Scanner user_input = new Scanner(System.in);
- int number;
- boolean validInput;
- do {
- System.out.print(prompt + ": ");
- try {
- number = Integer.parseInt(user_input.next());
- validInput = true;
- } catch (NumberFormatException e) {
- number = 0;
- System.out.print("Input wasn't an integer. ");
- validInput = false;
- }
- } while (!validInput);
- return number;
- }
- public static int integer() {
- return integer(DEFAULT_INT_PROMPT);
- }
- public static int intGreaterThan( String prompt, int min, String error ) {
- Scanner user_input = new Scanner(System.in);
- int number;
- do {
- if (min == Integer.MIN_VALUE) {
- System.out.print(prompt + ": ");
- }
- else {
- System.out.print(prompt + " (greater than " + min + "): ");
- }
- try {
- number = Integer.parseInt(user_input.next());
- if (number < min)
- {
- System.out.print(error + " ");
- }
- } catch (NumberFormatException e) {
- number = min - 1;
- System.out.print("Input wasn't an integer. ");
- }
- } while (number < min);
- return number;
- }
- public static int intGreaterThan( int min, String error ) {
- return intGreaterThan(DEFAULT_INT_PROMPT, min, error);
- }
- public static int intGreaterThan( int min ) {
- return intGreaterThan(min, "Input out of range.");
- }
- public static int wholeNumber( String error ) {
- return intGreaterThan(0, error);
- }
- public static int wholeNumber() {
- return wholeNumber("That's a negative number.");
- }
- public static int postiveInt( String error ) {
- return intGreaterThan(1, error);
- }
- public static int postiveInt() {
- return postiveInt("That's a negative number.");
- }
- public static int intLessThan( String prompt, int max, String error ) {
- Scanner user_input = new Scanner(System.in);
- int number;
- do {
- if (max == Integer.MAX_VALUE) {
- System.out.print(prompt + ": ");
- }
- else {
- System.out.print(prompt + " (less than " + max + "): ");
- }
- try {
- number = Integer.parseInt(user_input.next());
- if (number > max)
- {
- System.out.print(error + " ");
- }
- } catch (NumberFormatException e) {
- number = max + 1;
- System.out.print("Input wasn't an integer. ");
- }
- } while (number > max);
- return number;
- }
- public static int intLessThan( int max, String error ) {
- return intLessThan(DEFAULT_INT_PROMPT, max, error);
- }
- public static int intLessThan( int max ) {
- return intLessThan(max, "Input out of range.");
- }
- public static int intInRange( String prompt, int min, int max, String minError,
- String maxError ) {
- Scanner user_input = new Scanner(System.in);
- int number;
- do {
- if (min == Integer.MIN_VALUE && max == Integer.MAX_VALUE) {
- number = integer(prompt);
- }
- else if (min == Integer.MIN_VALUE) {
- number = intLessThan(prompt, max, maxError);
- }
- else if (max == Integer.MAX_VALUE) {
- number = intGreaterThan(prompt, min, minError);
- }
- else {
- System.out.print(prompt + " (between " + min + " and " + max + "): ");
- }
- try {
- number = Integer.parseInt(user_input.next());
- if (number < min) {
- System.out.print(minError + " ");
- }
- else if (number > max) {
- System.out.print(maxError + " ");
- }
- } catch (NumberFormatException e) {
- number = min - 1;
- System.out.print("Input wasn't an integer. ");
- }
- } while (number < min || number > max);
- return number;
- }
- public static int intInRange( int min, int max, String minError, String maxError ) {
- return intInRange(DEFAULT_INT_PROMPT, min, max, minError, maxError);
- }
- public static int intInRange( int min, int max, String error ) {
- return intInRange(min, max, error, error);
- }
- public static int intInRange( int min, int max ) {
- return intInRange(min, max, "Input out of range.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment