Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class IO {
- public static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- public static String readString() throws IOException {
- String text = bufferedReader.readLine();
- return text;
- }
- public static String readString(String prompt) throws IOException {
- System.out.print(prompt + ": ");
- String text = bufferedReader.readLine();
- return text;
- }
- public static int readInteger() throws IOException {
- while(true) {
- String text = bufferedReader.readLine();
- try {
- int number = Integer.parseInt(text);
- return number;
- } catch (NumberFormatException e) {
- System.out.println("Die Eingabe '" + text + "' ist keine Ganzzahl! Versuchen Sie es noch einmal");
- }
- }
- }
- public static int readInteger(String prompt) throws IOException {
- while(true) {
- System.out.println(prompt + ": ");
- String text = bufferedReader.readLine();
- try {
- int number = Integer.parseInt(text);
- return number;
- } catch (NumberFormatException e) {
- System.out.println("Die Eingabe '" + text + "' ist keine Ganzzahl! Versuchen Sie es noch einmal");
- }
- }
- }
- public static double readDouble() throws IOException {
- while (true) {
- String text = bufferedReader.readLine();
- try {
- double number = Double.parseDouble(text.trim().replace(',', '.'));
- return number;
- } catch (NumberFormatException e) {
- System.out.println("Die Eingabe '" + text + "' ist keine Kommazahl! Versuchen Sie es noch einmal.");
- }
- }
- }
- public static double readDouble(String prompt) throws IOException {
- while (true) {
- System.out.println(prompt + ": ");
- String text = bufferedReader.readLine();
- try {
- double number = Double.parseDouble(text.trim().replace(',', '.'));
- return number;
- } catch (NumberFormatException e) {
- System.out.println("Die Eingabe '" + text + "' ist keine Kommazahl! Versuchen Sie es noch einmal.");
- }
- }
- }
- public static boolean readBoolean() throws IOException {
- while (true) {
- String text = bufferedReader.readLine();
- if (text.equalsIgnoreCase("ja")
- || text.equalsIgnoreCase("j")
- || text.equalsIgnoreCase("yes")
- || text.equalsIgnoreCase("y")) {
- return true;
- }
- else if (text == null
- || text.equalsIgnoreCase("nein")
- || text.equalsIgnoreCase("n")
- || text.equalsIgnoreCase("no")) {
- return false;
- }
- else {
- System.out.print("Die Eingabe '" + text + "' ist keine gültige Eingabe! Versuchen Sie es noch einmal!");
- }
- }
- }
- public static boolean readBoolean(String prompt) throws IOException {
- while (true) {
- System.out.println(prompt + ": ");
- String text = bufferedReader.readLine();
- if (text.equalsIgnoreCase("ja")
- || text.equalsIgnoreCase("j")
- || text.equalsIgnoreCase("yes")
- || text.equalsIgnoreCase("y")) {
- return true;
- }
- else if (text == null
- || text.equalsIgnoreCase("nein")
- || text.equalsIgnoreCase("n")
- || text.equalsIgnoreCase("no")) {
- return false;
- }
- else {
- System.out.print("Die Eingabe '" + text + "' ist keine gültige Eingabe! Versuchen Sie es noch einmal!");
- }
- }
- }
- public static double[] readDoubleArray() throws IOException {
- System.out.println("Wie viele Werte sollen eingegeben werden? ");
- double[] array = IO.readDoubleArray();
- for (int i = 0; i < array.length; i++) {
- System.out.print("Geben Sie eine Ganzzahl ein: ");
- array[i] = IO.readDouble();
- }
- return array;
- }
- public static double[] readDoubleArray(int length) throws IOException {
- double[] array = new double[length];
- for (int i = 0; i < array.length; i++) {
- System.out.print("Geben Sie eine Ganzzahl ein: ");
- array[i] = IO.readDouble();
- }
- return array;
- }
- public static void writeDoubleArray(double[] array) throws IOException {
- for (int i = 0; i < array.length; i++) {
- System.out.println(array[i]);
- }
- }
- public static int[] readIntArray() throws IOException {
- System.out.println("Wie viele Werte sollen eingegeben werden? ");
- int[] array = IO.readIntArray();
- for (int i = 0; i < array.length; i++) {
- System.out.print("Geben Sie eine Ganzzahl ein: ");
- array[i] = IO.readInteger();
- }
- return array;
- }
- public static int[] readIntArray(int length) throws IOException {
- int[] array = new int[length];
- for (int i = 0; i < array.length; i++) {
- System.out.print("Geben Sie eine Ganzzahl ein: ");
- array[i] = IO.readInteger();
- }
- return array;
- }
- public static void writeIntArray(int[] array) throws IOException {
- for (int i = 0; i < array.length; i++) {
- System.out.println(array[i]);
- }
- }
- public static String[] readStringArray() throws IOException {
- System.out.println("Wie viele Werte sollen eingegeben werden? ");
- String[] array = IO.readStringArray();
- for (int i = 0; i < array.length; i++) {
- System.out.print("Geben Sie eine Ganzzahl ein: ");
- array[i] = IO.readString();
- }
- return array;
- }
- public static String[] readStringArray(int length) throws IOException {
- String[] array = new String[length];
- for (int i = 0; i < array.length; i++) {
- System.out.print("Geben Sie eine Ganzzahl ein: ");
- array[i] = IO.readString();
- }
- return array;
- }
- public static void writeStringArray(String[] array) throws IOException {
- for (int i = 0; i < array.length; i++) {
- System.out.println(array[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement