SHOW:
|
|
- or go back to the newest paste.
1 | // | |
2 | // Created by Julio Tentor <jtentor@fi.unju.edu.ar> | |
3 | // | |
4 | ||
5 | import java.util.Arrays; | |
6 | import java.util.Random; | |
7 | import java.util.Scanner; | |
8 | ||
9 | public class Helper { | |
10 | ||
11 | ||
12 | //region Static Objects | |
13 | static Random random = new Random(); | |
14 | static Scanner scanner = new Scanner(System.in); | |
15 | //endregion | |
16 | ||
17 | ||
18 | //region Character Helpers | |
19 | public static Character getCharacter(Scanner scanner, String inputMessage, String errorMessage) { | |
20 | Character characterValue; | |
21 | while (true) { | |
22 | System.out.print(inputMessage); | |
23 | try { | |
24 | characterValue = scanner.nextLine().charAt(0); | |
25 | return characterValue; | |
26 | } catch (Exception exception) { | |
27 | System.out.println(errorMessage); | |
28 | scanner.nextLine(); | |
29 | } | |
30 | } | |
31 | } | |
32 | public static Character getCharacter(Scanner scanner, String inputMessage) { | |
33 | return getCharacter(scanner, inputMessage, "Ingrese un caracter válido"); | |
34 | } | |
35 | public static Character getCharacter(String inputMessage, String errorMessage) { | |
36 | return getCharacter(Helper.scanner, inputMessage, errorMessage); | |
37 | } | |
38 | public static Character getCharacter(String inputMessage) { | |
39 | return getCharacter(Helper.scanner, inputMessage, "Ingrese un caracter válido"); | |
40 | } | |
41 | //endregion | |
42 | ||
43 | ||
44 | //region Integer Helpers | |
45 | public static Integer getInteger(Scanner scanner, String inputMessage, String errorMessage) { | |
46 | Integer integerValue = 0; | |
47 | while (true) { | |
48 | try { | |
49 | System.out.print(inputMessage); | |
50 | integerValue = Integer.parseInt(scanner.nextLine()); | |
51 | return integerValue; | |
52 | } | |
53 | catch (Exception exception) { | |
54 | System.out.println(errorMessage); | |
55 | } | |
56 | } | |
57 | } | |
58 | public static Integer getInteger(Scanner scanner, String inputMessage) { | |
59 | return getInteger(scanner, inputMessage, "Ingrese un número válido"); | |
60 | } | |
61 | public static Integer getInteger(String inputMessage, String errorMessage) { | |
62 | return getInteger(Helper.scanner, inputMessage, errorMessage); | |
63 | } | |
64 | public static Integer getInteger(String inputMessage) { | |
65 | return getInteger(Helper.scanner, inputMessage, "Ingrese un número válido"); | |
66 | } | |
67 | //endregion | |
68 | ||
69 | ||
70 | //region Double Helpers | |
71 | public static Double getDouble(Scanner scanner, String inputMessage, String errorMessage) { | |
72 | Double doubleValue = 0.0; | |
73 | while (true) { | |
74 | try { | |
75 | System.out.print(inputMessage); | |
76 | doubleValue = Double.parseDouble(scanner.nextLine()); | |
77 | return doubleValue; | |
78 | } | |
79 | catch (Exception exception) { | |
80 | System.out.println(errorMessage); | |
81 | } | |
82 | } | |
83 | } | |
84 | public static Double getDouble(Scanner scanner, String inputMessage) { | |
85 | return getDouble(scanner, inputMessage, "Ingrese un número válido"); | |
86 | } | |
87 | public static Double getDouble(String inputMessage, String errorMessage) { | |
88 | return getDouble(Helper.scanner, inputMessage, errorMessage); | |
89 | } | |
90 | public static Double getDouble(String inputMessage) { | |
91 | return getDouble(Helper.scanner, inputMessage, "Ingrese un número válido"); | |
92 | } | |
93 | //endregion | |
94 | ||
95 | ||
96 | //region Float Helpers | |
97 | public static Float getFloat(Scanner scanner, String inputMessage, String errorMessage) { | |
98 | Float floatValue = 0f; | |
99 | while (true) { | |
100 | try { | |
101 | System.out.print(inputMessage); | |
102 | floatValue = Float.parseFloat(scanner.nextLine()); | |
103 | return floatValue; | |
104 | } | |
105 | catch (Exception exception) { | |
106 | System.out.println(errorMessage); | |
107 | } | |
108 | } | |
109 | } | |
110 | public static Float getFloat(Scanner scanner, String inputMessage) { | |
111 | return getFloat(scanner, inputMessage, "Ingrese un número válido"); | |
112 | } | |
113 | public static Float getFloat(String inputMessage, String errorMessage) { | |
114 | return getFloat(Helper.scanner, inputMessage, errorMessage); | |
115 | } | |
116 | public static Float getFloat(String inputMessage) { | |
117 | return getFloat(Helper.scanner, inputMessage, "Ingrese un número válido"); | |
118 | } | |
119 | //endregion | |
120 | ||
121 | ||
122 | ||
123 | //region Array Helpers | |
124 | ||
125 | static void printOneDimensionArray(String textBefore, Object[] array, String textAfter) { | |
126 | System.out.print(textBefore); | |
127 | System.out.print("[" + array[0]); | |
128 | for (int i = 1; i < array.length; ++i) { | |
129 | System.out.print("," + array[i]); | |
130 | } | |
131 | System.out.print("]"); | |
132 | System.out.print(textAfter); | |
133 | } | |
134 | ||
135 | ||
136 | static void printTwoDimensionArray(String textBefore, Object[][] array, String textAfter) { | |
137 | System.out.print(textBefore); | |
138 | ||
139 | System.out.print("[[" + array[0][0]); | |
140 | for (int j = 1; j < array[0].length; ++j) { | |
141 | System.out.print("," + array[0][j]); | |
142 | } | |
143 | System.out.print("]"); | |
144 | ||
145 | for (int i = 1; i < array.length; ++i) { | |
146 | System.out.print(",[" + array[i][0]); | |
147 | for (int j = 1; j < array[i].length; ++j) { | |
148 | System.out.print("," + array[i][j]); | |
149 | } | |
150 | System.out.print("]"); | |
151 | } | |
152 | System.out.print("]"); | |
153 | System.out.print(textAfter); | |
154 | } | |
155 | //endregion | |
156 | ||
157 | ||
158 | ||
159 | //region Enum Helpers | |
160 | ||
161 | // from http://stackoverflow.com/questions/13783295/getting-all-names-in-an-enum-as-a-string | |
162 | public static String[] getEnumNames(Class<? extends Enum<?>> e) { | |
163 | return Arrays.toString(e.getEnumConstants()).replaceAll("^.|.$", "").split(", "); | |
164 | } | |
165 | ||
166 | //endregion | |
167 | ||
168 | } | |
169 |