import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** Klasse Input * Liest von Standardeingabe verschiedene Datentypen ein. * Z.b. liest Input.in.readInteger() eine Integer ein und * gibt diese zurück. * Mit Input.in.hasSucceeded() kann abgerufen werden, ob * der Einlesevorgang geglückt ist. * @author niki * */ public class Input { /** * Speichert den Status ab. */ private boolean success; /** * Das Einleseobjekt */ public final static Input in = new Input(); private Input() {} /** * War der Einlesevorgang erfolgreich? * @return Ja falls true */ boolean hasSucceeded() { return success; } private String readLine() { success = true; try { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); return r.readLine(); } catch(IOException e) { e.printStackTrace(); } success = false; return null; } byte readByte() { String s = readLine(); try { return (s == null) ? 0 : Byte.parseByte(s); } catch(NumberFormatException e) { e.printStackTrace(); } success = false; return 0; } short readShort() { String s = readLine(); try { return (s == null) ? 0 : Short.parseShort(s); } catch(NumberFormatException e) { e.printStackTrace(); } success = false; return 0; } int readInteger() { String s = readLine(); try { return (s == null) ? 0 : Integer.parseInt(s); } catch(NumberFormatException e) { e.printStackTrace(); } success = false; return 0; } long readLong() { String s = readLine(); try { return (s == null) ? 0 : Long.parseLong(s); } catch(java.lang.NumberFormatException e) { e.printStackTrace(); } success = false; return 0; } float readFloat() { String s = readLine(); try { return (s == null) ? 0 : Float.parseFloat(s); } catch(java.lang.NumberFormatException e) { e.printStackTrace(); } success = false; return 0; } double readDouble() { String s = readLine(); try { return (s == null) ? 0 : Double.parseDouble(s); } catch(java.lang.NumberFormatException e) { e.printStackTrace(); } success = false; return 0; } String readString() { return readLine(); } char readCharacter() { success = true; try { char c = 0; while(c == 0) { int r = System.in.read(); if(r == -1) { success = false; return 0; } if(r>31) c = (char)r; } System.in.skip(System.in.available()); // Dismiss Newline & Carriage-Return return c; } catch(IOException e) { e.printStackTrace(); } success = false; return 0; } }