reisi007

Input.java

Oct 21st, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Created with IntelliJ IDEA.
  5.  * User: Florian Reisinger
  6.  */
  7. public class Input {
  8.     private static Scanner console = new Scanner(System.in);
  9.     private static Boolean lastWorking = true;
  10.  
  11.     public static String getString() {
  12.         return console.nextLine();
  13.     }
  14.  
  15.     public static int getInt() {
  16.         int i = 0;
  17.         do {
  18.             try {
  19.                 i = Integer.parseInt(getString());
  20.                 lastWorking = true;
  21.             } catch (Exception e) {
  22.                 System.out.println("No valid int!");
  23.                 System.out.print("Retry: ");
  24.                 lastWorking = false;
  25.             }
  26.         } while (!lastWorking);
  27.         return i;
  28.  
  29.     }
  30.  
  31.     public static float getFloat() {
  32.         float i = 0;
  33.         do {
  34.             try {
  35.                 i = Float.parseFloat(getString());
  36.                 lastWorking = true;
  37.             } catch (Exception e) {
  38.                 System.out.println("No valid float! Please use \".\" instead of \",\"");
  39.                 System.out.print("Retry: ");
  40.                 lastWorking = false;
  41.             }
  42.         } while (!lastWorking);
  43.         return i;
  44.  
  45.     }
  46.  
  47.     public static double getDouble() {
  48.         double i = 0;
  49.         do {
  50.             try {
  51.                 i = Double.parseDouble(getString());
  52.                 lastWorking = true;
  53.             } catch (Exception e) {
  54.                 System.out.println("No valid double! Please use \".\" instead of \",\"");
  55.                 System.out.print("Retry: ");
  56.                 lastWorking = false;
  57.             }
  58.         } while (!lastWorking);
  59.         return i;
  60.  
  61.     }
  62.  
  63.     public static long getLong() {
  64.         long i = 0;
  65.         do {
  66.             try {
  67.                 i = Long.getLong(getString());
  68.                 lastWorking = true;
  69.             } catch (Exception e) {
  70.                 System.out.println("No valid long");
  71.                 System.out.print("Retry: ");
  72.                 lastWorking = false;
  73.             }
  74.         } while (!lastWorking);
  75.         return i;
  76.  
  77.     }
  78.  
  79.     public static byte getByte() {
  80.         byte i = 0;
  81.         do {
  82.             try {
  83.                 i = Byte.decode(getString());
  84.                 lastWorking = true;
  85.             } catch (Exception e) {
  86.                 System.out.println("No valid byte.");
  87.                 System.out.print("Retry: ");
  88.                 lastWorking = false;
  89.             }
  90.         } while (!lastWorking);
  91.         return i;
  92.  
  93.     }
  94.  
  95.     private static String[] getWordHelper(String source) {
  96.         return source.split(" ");
  97.     }
  98.  
  99.     public static String[] getWords() {
  100.         return getWordHelper(getString());
  101.     }
  102.  
  103.     public static String getFirstWord() {
  104.         return getWordHelper(getString())[0];
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment