Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class IO {
  6.  
  7. public static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  8.  
  9. public static String readString() throws IOException {
  10. String text = bufferedReader.readLine();
  11. return text;
  12. }
  13.  
  14. public static String readString(String prompt) throws IOException {
  15. System.out.print(prompt + ": ");
  16. String text = bufferedReader.readLine();
  17. return text;
  18. }
  19.  
  20. public static int readInteger() throws IOException {
  21. while(true) {
  22. String text = bufferedReader.readLine();
  23. try {
  24. int number = Integer.parseInt(text);
  25. return number;
  26. } catch (NumberFormatException e) {
  27. System.out.println("Die Eingabe '" + text + "' ist keine Ganzzahl! Versuchen Sie es noch einmal");
  28. }
  29. }
  30. }
  31.  
  32. public static int readInteger(String prompt) throws IOException {
  33. while(true) {
  34. System.out.println(prompt + ": ");
  35. String text = bufferedReader.readLine();
  36. try {
  37. int number = Integer.parseInt(text);
  38. return number;
  39. } catch (NumberFormatException e) {
  40. System.out.println("Die Eingabe '" + text + "' ist keine Ganzzahl! Versuchen Sie es noch einmal");
  41. }
  42. }
  43. }
  44.  
  45. public static double readDouble() throws IOException {
  46. while (true) {
  47. String text = bufferedReader.readLine();
  48. try {
  49. double number = Double.parseDouble(text.trim().replace(',', '.'));
  50. return number;
  51. } catch (NumberFormatException e) {
  52. System.out.println("Die Eingabe '" + text + "' ist keine Kommazahl! Versuchen Sie es noch einmal.");
  53. }
  54. }
  55. }
  56.  
  57. public static double readDouble(String prompt) throws IOException {
  58. while (true) {
  59. System.out.println(prompt + ": ");
  60. String text = bufferedReader.readLine();
  61. try {
  62. double number = Double.parseDouble(text.trim().replace(',', '.'));
  63. return number;
  64. } catch (NumberFormatException e) {
  65. System.out.println("Die Eingabe '" + text + "' ist keine Kommazahl! Versuchen Sie es noch einmal.");
  66. }
  67. }
  68. }
  69.  
  70. public static boolean readBoolean() throws IOException {
  71. while (true) {
  72. String text = bufferedReader.readLine();
  73. if (text.equalsIgnoreCase("ja")
  74. || text.equalsIgnoreCase("j")
  75. || text.equalsIgnoreCase("yes")
  76. || text.equalsIgnoreCase("y")) {
  77. return true;
  78. }
  79. else if (text == null
  80. || text.equalsIgnoreCase("nein")
  81. || text.equalsIgnoreCase("n")
  82. || text.equalsIgnoreCase("no")) {
  83. return false;
  84. }
  85. else {
  86. System.out.print("Die Eingabe '" + text + "' ist keine gültige Eingabe! Versuchen Sie es noch einmal!");
  87. }
  88. }
  89. }
  90.  
  91. public static boolean readBoolean(String prompt) throws IOException {
  92. while (true) {
  93. System.out.println(prompt + ": ");
  94. String text = bufferedReader.readLine();
  95. if (text.equalsIgnoreCase("ja")
  96. || text.equalsIgnoreCase("j")
  97. || text.equalsIgnoreCase("yes")
  98. || text.equalsIgnoreCase("y")) {
  99. return true;
  100. }
  101. else if (text == null
  102. || text.equalsIgnoreCase("nein")
  103. || text.equalsIgnoreCase("n")
  104. || text.equalsIgnoreCase("no")) {
  105. return false;
  106. }
  107. else {
  108. System.out.print("Die Eingabe '" + text + "' ist keine gültige Eingabe! Versuchen Sie es noch einmal!");
  109. }
  110. }
  111. }
  112.  
  113. public static double[] readDoubleArray() throws IOException {
  114. System.out.println("Wie viele Werte sollen eingegeben werden? ");
  115. double[] array = IO.readDoubleArray();
  116. for (int i = 0; i < array.length; i++) {
  117. System.out.print("Geben Sie eine Ganzzahl ein: ");
  118. array[i] = IO.readDouble();
  119. }
  120. return array;
  121. }
  122.  
  123. public static double[] readDoubleArray(int length) throws IOException {
  124. double[] array = new double[length];
  125.  
  126. for (int i = 0; i < array.length; i++) {
  127. System.out.print("Geben Sie eine Ganzzahl ein: ");
  128. array[i] = IO.readDouble();
  129. }
  130. return array;
  131. }
  132.  
  133. public static void writeDoubleArray(double[] array) throws IOException {
  134. for (int i = 0; i < array.length; i++) {
  135. System.out.println(array[i]);
  136. }
  137. }
  138.  
  139. public static int[] readIntArray() throws IOException {
  140. System.out.println("Wie viele Werte sollen eingegeben werden? ");
  141. int[] array = IO.readIntArray();
  142. for (int i = 0; i < array.length; i++) {
  143. System.out.print("Geben Sie eine Ganzzahl ein: ");
  144. array[i] = IO.readInteger();
  145. }
  146. return array;
  147. }
  148.  
  149. public static int[] readIntArray(int length) throws IOException {
  150. int[] array = new int[length];
  151.  
  152. for (int i = 0; i < array.length; i++) {
  153. System.out.print("Geben Sie eine Ganzzahl ein: ");
  154. array[i] = IO.readInteger();
  155. }
  156. return array;
  157. }
  158.  
  159. public static void writeIntArray(int[] array) throws IOException {
  160. for (int i = 0; i < array.length; i++) {
  161. System.out.println(array[i]);
  162. }
  163. }
  164.  
  165. public static String[] readStringArray() throws IOException {
  166. System.out.println("Wie viele Werte sollen eingegeben werden? ");
  167. String[] array = IO.readStringArray();
  168. for (int i = 0; i < array.length; i++) {
  169. System.out.print("Geben Sie eine Ganzzahl ein: ");
  170. array[i] = IO.readString();
  171. }
  172. return array;
  173. }
  174.  
  175. public static String[] readStringArray(int length) throws IOException {
  176. String[] array = new String[length];
  177.  
  178. for (int i = 0; i < array.length; i++) {
  179. System.out.print("Geben Sie eine Ganzzahl ein: ");
  180. array[i] = IO.readString();
  181. }
  182. return array;
  183. }
  184.  
  185. public static void writeStringArray(String[] array) throws IOException {
  186. for (int i = 0; i < array.length; i++) {
  187. System.out.println(array[i]);
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement