Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Created with IntelliJ IDEA.
- * User: Florian Reisinger
- */
- public class Input {
- private static Scanner console = new Scanner(System.in);
- private static Boolean lastWorking = true;
- public static String getString() {
- return console.nextLine();
- }
- public static int getInt() {
- int i = 0;
- do {
- try {
- i = Integer.parseInt(getString());
- lastWorking = true;
- } catch (Exception e) {
- System.out.println("No valid int!");
- System.out.print("Retry: ");
- lastWorking = false;
- }
- } while (!lastWorking);
- return i;
- }
- public static float getFloat() {
- float i = 0;
- do {
- try {
- i = Float.parseFloat(getString());
- lastWorking = true;
- } catch (Exception e) {
- System.out.println("No valid float! Please use \".\" instead of \",\"");
- System.out.print("Retry: ");
- lastWorking = false;
- }
- } while (!lastWorking);
- return i;
- }
- public static double getDouble() {
- double i = 0;
- do {
- try {
- i = Double.parseDouble(getString());
- lastWorking = true;
- } catch (Exception e) {
- System.out.println("No valid double! Please use \".\" instead of \",\"");
- System.out.print("Retry: ");
- lastWorking = false;
- }
- } while (!lastWorking);
- return i;
- }
- public static long getLong() {
- long i = 0;
- do {
- try {
- i = Long.getLong(getString());
- lastWorking = true;
- } catch (Exception e) {
- System.out.println("No valid long");
- System.out.print("Retry: ");
- lastWorking = false;
- }
- } while (!lastWorking);
- return i;
- }
- public static byte getByte() {
- byte i = 0;
- do {
- try {
- i = Byte.decode(getString());
- lastWorking = true;
- } catch (Exception e) {
- System.out.println("No valid byte.");
- System.out.print("Retry: ");
- lastWorking = false;
- }
- } while (!lastWorking);
- return i;
- }
- private static String[] getWordHelper(String source) {
- return source.split(" ");
- }
- public static String[] getWords() {
- return getWordHelper(getString());
- }
- public static String getFirstWord() {
- return getWordHelper(getString())[0];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment