Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. import java.time.* ;
  2. import java.time.format.DateTimeParseException;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Input
  7. {
  8. private static Scanner scan = new Scanner(System.in);
  9.  
  10. public static void UseEnterAsDelimiter()
  11. {
  12. scan.useDelimiter(System.getProperty("line.separator"));
  13. }
  14.  
  15. public static int IntInRange(int min , int max, String InvalidMsg )
  16. {
  17. int value;
  18.  
  19. while(true)
  20. {
  21. while (!scan.hasNextInt())
  22. {
  23. System.out.println("Expected an Integer. Please type again.");
  24. scan.next();
  25. }
  26.  
  27. value = scan.nextInt();
  28.  
  29. if( value >= min && value <= max)
  30. return value;
  31. else
  32. {
  33. System.out.println(InvalidMsg);
  34. }
  35. }
  36. }
  37.  
  38. public static int Int()
  39. {
  40. int value;
  41.  
  42. while (true)
  43. {
  44. while (!scan.hasNextInt())
  45. {
  46. System.out.println("Expected an Integer. Please type again.");
  47. scan.next();
  48. }
  49.  
  50. value = scan.nextInt();
  51.  
  52. return value;
  53. }
  54. }
  55.  
  56. public static String String()
  57. {
  58. return scan.next();
  59. }
  60.  
  61. public static String StringNoEmpty()
  62. {
  63. String value;
  64.  
  65. while(true)
  66. {
  67. value = scan.next();
  68. if ( !value.isEmpty())
  69. return value;
  70. else
  71. System.out.println("Input cannot be blank. Please type again.");
  72. }
  73.  
  74. }
  75.  
  76. public static LocalDate Date()
  77. {
  78. LocalDate value;
  79.  
  80. while(true)
  81. {
  82. try
  83. {
  84. String input = scan.next();
  85. if( input.isEmpty())
  86. System.out.println("Field cannot be empty.");
  87. else
  88. {
  89. value = LocalDate.parse(input);
  90. return value;
  91. }
  92. }
  93. catch(DateTimeParseException e)
  94. {
  95. System.out.println("Date was not given in a correct format , please type it again.");
  96. }
  97. }
  98. }
  99.  
  100. public static LocalTime Time()
  101. {
  102. LocalTime value;
  103.  
  104. while(true)
  105. {
  106. try
  107. {
  108. String input = scan.next();
  109. if( input.isEmpty())
  110. System.out.println("Field cannot be empty.");
  111. else
  112. {
  113. value = LocalTime.parse(input);
  114. return value;
  115. }
  116. }
  117. catch(DateTimeParseException e)
  118. {
  119. System.out.println("Time was not given in a correct format , please type it again.");
  120. }
  121. }
  122. }
  123.  
  124. public static void CloseScanner()
  125. {
  126. scan.close();
  127. }
  128.  
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement