Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.* ;
- import java.time.format.DateTimeParseException;
- import java.util.Scanner;
- public class Input
- {
- private static Scanner scan = new Scanner(System.in);
- public static void UseEnterAsDelimiter()
- {
- scan.useDelimiter(System.getProperty("line.separator"));
- }
- public static int IntInRange(int min , int max, String InvalidMsg )
- {
- int value;
- while(true)
- {
- while (!scan.hasNextInt())
- {
- System.out.println("Expected an Integer. Please type again.");
- scan.next();
- }
- value = scan.nextInt();
- if( value >= min && value <= max)
- return value;
- else
- {
- System.out.println(InvalidMsg);
- }
- }
- }
- public static int Int()
- {
- int value;
- while (true)
- {
- while (!scan.hasNextInt())
- {
- System.out.println("Expected an Integer. Please type again.");
- scan.next();
- }
- value = scan.nextInt();
- return value;
- }
- }
- public static String String()
- {
- return scan.next();
- }
- public static String StringNoEmpty()
- {
- String value;
- while(true)
- {
- value = scan.next();
- if ( !value.isEmpty())
- return value;
- else
- System.out.println("Input cannot be blank. Please type again.");
- }
- }
- public static LocalDate Date()
- {
- LocalDate value;
- while(true)
- {
- try
- {
- String input = scan.next();
- if( input.isEmpty())
- System.out.println("Field cannot be empty.");
- else
- {
- value = LocalDate.parse(input);
- return value;
- }
- }
- catch(DateTimeParseException e)
- {
- System.out.println("Date was not given in a correct format , please type it again.");
- }
- }
- }
- public static LocalTime Time()
- {
- LocalTime value;
- while(true)
- {
- try
- {
- String input = scan.next();
- if( input.isEmpty())
- System.out.println("Field cannot be empty.");
- else
- {
- value = LocalTime.parse(input);
- return value;
- }
- }
- catch(DateTimeParseException e)
- {
- System.out.println("Time was not given in a correct format , please type it again.");
- }
- }
- }
- public static void CloseScanner()
- {
- scan.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement