001 package de.hska.java.aufgaben.kontrollstrukturen;
002
003
004 import java.io.BufferedReader;
005 import java.io.IOException;
006 import java.io.InputStreamReader;
007
008 /**
009 * Hilfeklasse zum Einlesen von Zeichenketten und Zahlen von der Konsole.
010 *
011 * @author Christian Pape
012 */
013 public class Eingabe {
014
015 private static BufferedReader console = new BufferedReader( new InputStreamReader( System.in ) );
016
017 /**
018 * Gibt die nächste Eingabezeile als String zurück.
019 */
020 public static String readLine() {
021 try {
022 return console.readLine();
023 } catch (IOException e) {
024 return "\n";
025 }
026 }
027
028 /**
029 * Gibt die nächste Eingabezeile als int-Wert zurück. Es werden
030 * nur die ersten Ziffern inklusive Vorzeichen berücksichtigt.
031 */
032 public static int readInt() {
033 return parseInt(readLine());
034 }
035
036 /**
037 * Wandelt <code>zahl</code> in einen int-Wert.
038 */
039 public static int parseInt(String zahl) {
040 try {
041 return Integer.parseInt(zahl);
042 } catch (NumberFormatException e) {
043 return 0;
044 }
045 }
046
047 /**
048 * Liest eine Gleitkommazahl von der Tastatur ein
049 * und gibt ihn als double-Wert zurück.
050 */
051 public static double readDouble() {
052 return Double.valueOf(Eingabe.readLine());
053 }
054 }