Advertisement
Nickpips

Untitled

Nov 28th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. package main;
  2.  
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5.  
  6. public class MyScanner {
  7. Scanner scan;
  8. public MyScanner( Scanner scan )
  9. {
  10. this.scan = scan;
  11. }
  12. public String getLine()
  13. {
  14. String ret = "";
  15. try {
  16. ret = scan.nextLine();
  17. } catch( Exception e ) {
  18. System.out.println("Exception: " + e);
  19. return "";
  20. }
  21. return ret;
  22. }
  23. public int getInt(int a, int b) {
  24. int input = getInt();
  25. while( input < a || input > b ) {
  26. System.out.println("You must pick a number in-between " + a + " and " + b + "!");
  27. input = getInt();
  28. }
  29. return input;
  30. }
  31.  
  32. public int getInt() {
  33. int output = 0;
  34. while( true ) {
  35. try {
  36. output = scan.nextInt();
  37. } catch( InputMismatchException e ) {
  38. System.out.printf("Please input an integer.\n");
  39. scan.nextLine();
  40. continue;
  41. } catch( Exception e ) {
  42. System.out.println("Exception: " + e);
  43. return 0;
  44. }
  45. break;
  46. }
  47. scan.nextLine();
  48. return output;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement