-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.java
More file actions
526 lines (462 loc) · 15.1 KB
/
IO.java
File metadata and controls
526 lines (462 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/**
*A short description : Class for simplified GUI Dialog input of all variable types .....Getting GUI Dialog input
*in java is a very tedious process ! This java
*class will help you to get all types of variables input from GUI Dialog with relatively ease.
*All nine fundamental variable types are covered : byte, short, int, long, float, double, char, String, and
*boolean . It ignores appropiate input errors smartly. Copy this file to your project/java directory and
*name it IO.java . Compile it using command : javac IO.java , and you are all done. In BlueJ
*Save the source file, then use the "Import Class" function in BlueJ to import this class into your project.
*Then it can be used like any other class. Please see the use of this class and input methods in
*testguinput.java class example below, if you enjoy my effort, let me know through e-mail !!!
*
* Original Authors : Bruce Quig + Michael Kolling, version: 1.0, Date: 04.03.1999
* @author (Modified and enhanced by Shirshasin Ghosh Of Naihati, West Bengal, India... shirsha@rediffmail.com )
* @author (Modified by Tino Hempel)
* @version (2.1: 6.3.2006)
*/
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JDialog;
import javax.swing.JButton;
import java.awt.event.*;
public class IO {
/* instance variables */
private static final String FENSTER_TITEL = "Eingabe";
private static final String STRING_TITLE = "Geben Sie eine Zeichenkette ein.";
private static final String BYTE_TITLE = "Geben Sie eine ganze Zahl ein.";
private static final String CHAR_TITLE = "Geben Sie ein Zeichen ein";
private static final String SHORT_TITLE = "Geben Sie eine ganze Zahl ein";
private static final String LONG_TITLE = "Geben Sie eine ganze Zahl ein";
private static final String INT_TITLE = "Geben Sie eine ganze Zahl ein.";
private static final String BOOLEAN_TITLE = "W?hlen Sie ja oder nein.";
private static final String FLOAT_TITLE = "Geben Sie eine Dezimalzahl ein.";
private static final String DOUBLE_TITLE = "Geben Sie eine Dezimalzahl ein";
private static final String ERROR_TITLE = "Unzul?ssige Eingabe ";
private static final String TRUE = "Ja";
private static final String FALSE = "Nein";
private static final String EMPTY_STRING = "";
private static int i, j ;
private static String hold = "" ;
private static boolean checkecc = false ;
/**
* Eingabe vom Typ byte
* @param prompt Hinweistext als String
* @return byte Eingabe als Wert vom Typ byte
*/
public static byte getByte(String prompt)
{
Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};
Object[] options = { "OK" };
String inputValue = "";
boolean validResponse = false;
byte response = 0;
while(!validResponse)
{
final JOptionPane optionPane = new JOptionPane(commentArray,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[0]);
optionPane.setWantsInput(true);
final JDialog dialog = optionPane.createDialog(null, FENSTER_TITEL);
dialog.pack();
dialog.setVisible(true);
String result = (String)optionPane.getInputValue();
if ( checkecc ) result = convertwhole(result) ;
try
{
response = Byte.parseByte(result);
validResponse = true;
dialog.dispose();
}
catch(NumberFormatException exception)
{
if(result.equals("uninitializedValue")) result = "";
commentArray[1] = ERROR_TITLE+result;
commentArray[2] = BYTE_TITLE;
}
}
return response;
} /* End of getByte method */
/**
* Eingabe vom Typ short
* @param prompt Hinweistext als String
* @return short Eingabe als Wert vom Typ short
*/
public static short getShort(String prompt)
{
Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};
Object[] options = { "OK" };
String inputValue = "";
boolean validResponse = false;
short response = 0;
while(!validResponse)
{
final JOptionPane optionPane = new JOptionPane(commentArray,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[0]);
optionPane.setWantsInput(true);
final JDialog dialog = optionPane.createDialog(null, FENSTER_TITEL);
dialog.pack();
dialog.setVisible(true);
String result = (String)optionPane.getInputValue();
if ( checkecc ) result = convertwhole(result) ;
try
{
response = Short.parseShort(result);
validResponse = true;
dialog.dispose();
}
catch(NumberFormatException exception)
{
if(result.equals("uninitializedValue")) result = "";
commentArray[1] = ERROR_TITLE+result;
commentArray[2] = SHORT_TITLE;
}
}
return response;
} /* End of getShort method */
/**
* Eingabe vom Typ int
* @param prompt Hinweistext als String
* @return int Eingabe als Wert vom Typ int
*/
public static int getInt(String prompt)
{
Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};
Object[] options = { "OK" };
String inputValue = "";
boolean validResponse = false;
int response = 0;
while(!validResponse)
{
final JOptionPane optionPane = new JOptionPane(commentArray,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[0]);
optionPane.setWantsInput(true);
final JDialog dialog = optionPane.createDialog(null, FENSTER_TITEL);
dialog.pack();
dialog.setVisible(true);
String result = (String)optionPane.getInputValue();
if ( checkecc ) result = convertwhole(result) ;
try
{
response = Integer.parseInt(result);
validResponse = true;
dialog.dispose();
}
catch(NumberFormatException exception)
{
if(result.equals("uninitializedValue")) result = "";
commentArray[1] = ERROR_TITLE+result;
commentArray[2] = INT_TITLE;
}
}
return response;
} /* End of getInt method */
/**
* Eingabe vom Typ long
* @param prompt Hinweistext als String
* @return long Eingabe als Wert vom Typ long
*/
public static long getLong(String prompt)
{
Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};
Object[] options = { "OK" };
String inputValue = "";
boolean validResponse = false;
long response = 0;
while(!validResponse)
{
final JOptionPane optionPane = new JOptionPane(commentArray,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[0]);
optionPane.setWantsInput(true);
final JDialog dialog = optionPane.createDialog(null, FENSTER_TITEL);
dialog.pack();
dialog.setVisible(true);
String result = (String)optionPane.getInputValue();
if ( checkecc ) result = convertwhole(result) ;
try
{
response = Long.parseLong(result);
validResponse = true;
dialog.dispose();
}
catch(NumberFormatException exception)
{
if(result.equals("uninitializedValue")) result = "";
commentArray[1] = ERROR_TITLE+result;
commentArray[2] = LONG_TITLE;
}
}
return response;
} /* End of getByte method */
/**
* Eingabe vom Typ float
* @param prompt Hinweistext als String
* @return float Eingabe als Wert vom Typ float
*/
public static float getFloat(String prompt)
{
Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};
Object[] options = { "OK" };
String inputValue = "";
boolean validResponse = false;
float response = 0.0f;
while(!validResponse)
{
final JOptionPane optionPane = new JOptionPane(commentArray,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[0]);
optionPane.setWantsInput(true);
final JDialog dialog = optionPane.createDialog(null, FENSTER_TITEL);
dialog.pack();
dialog.setVisible(true);
String result = (String)optionPane.getInputValue();
if ( checkecc ) result = convertfrac(result) ;
try
{
response = Float.parseFloat(result);
validResponse = true;
dialog.dispose();
}
catch(NumberFormatException exception)
{
if(result.equals("uninitializedValue")) result = "";
commentArray[1] = ERROR_TITLE+result;
commentArray[2] = FLOAT_TITLE;
}
}
return response;
} /* End of getByte method */
/**
* Eingabe vom Typ double
* @param prompt Hinweistext als String
* @return double Eingabe als Wert vom Typ double
*/
public static double getDouble(String prompt)
{
Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};
Object[] options = { "OK" };
String inputValue = "";
boolean validResponse = false;
double response = 0;
while(!validResponse)
{
final JOptionPane optionPane = new JOptionPane(commentArray,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[0]);
optionPane.setWantsInput(true);
final JDialog dialog = optionPane.createDialog(null, FENSTER_TITEL);
dialog.pack();
dialog.setVisible(true);
String result = (String)optionPane.getInputValue();
if ( checkecc ) result = convertfrac(result) ;
try
{
response = Double.parseDouble(result);
validResponse = true;
dialog.dispose();
}
catch(NumberFormatException exception)
{
if(result.equals("uninitializedValue")) result = "";
commentArray[1] = ERROR_TITLE+result;
commentArray[2] = DOUBLE_TITLE;
}
}
return response;
} /* End of getByte method */
/**
* Eingabe vom Typ char
* @param prompt Hinweistext als String
* @return char Eingabe als Wert vom Typ char
*/
public static char getChar(String prompt)
{
char response ='-';
String result = null;
Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};
Object[] options = { "OK" };
String inputValue = "";
boolean validResponse = false;
while(!validResponse)
{
final JOptionPane optionPane = new JOptionPane(commentArray,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[0]);
optionPane.setWantsInput(true);
final JDialog dialog = optionPane.createDialog(null, prompt);
dialog.pack();
dialog.setVisible(true);
Object input = optionPane.getInputValue();
if(input != JOptionPane.UNINITIALIZED_VALUE)
{
result = (String)input;
if(result != null && result.length() == 1)
{
response = result.charAt(0);
validResponse = true;
dialog.dispose();
}
else
{
commentArray[1] = ERROR_TITLE + result;
commentArray[2] = CHAR_TITLE;
}
}
else
{
commentArray[1] = ERROR_TITLE + result;
commentArray[2] = CHAR_TITLE;
}
}
return response;
} /* End of getChar method */
/**
* Eingabe vom Typ String
* @param prompt Hinweistext als String
* @return String Eingabe als Wert vom Typ String
*/
public static String getString(String prompt)
{
Object[] commentArray = {prompt, EMPTY_STRING};
Object[] options = { "OK" };
String inputValue = "";
boolean validResponse = false;
String result = null;
while(!validResponse)
{
final JOptionPane optionPane = new JOptionPane(commentArray,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[0]);
optionPane.setWantsInput(true);
final JDialog dialog = optionPane.createDialog(null, FENSTER_TITEL);
dialog.pack();
dialog.setVisible(true);
Object response = optionPane.getInputValue();
if(response != JOptionPane.UNINITIALIZED_VALUE)
{
result = (String)response;
validResponse = true;
dialog.dispose();
}
else
{
commentArray[1] = ERROR_TITLE + result;
commentArray[2] = STRING_TITLE;
}
}
return result;
} /* End of getString method */
/**
* Eingabe vom Typ boolean
* @param prompt Hinweistext als String
* @param trueText Text auf der True-Schaltfl?che
* @param falseText Text auf der False-Schaltfl?che
* @return boolean Eingabe als Wert vom Typ boolean
*/
public static boolean getBoolean(String prompt, String trueText, String falseText)
{
Object[] commentArray = {prompt, EMPTY_STRING};
boolean validResponse = false;
int result = -1;
while(!validResponse)
{
Object[] options = {trueText, falseText};
result = JOptionPane.showOptionDialog(null,
commentArray,
FENSTER_TITEL,
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //don't use a custom Icon
options, //the titles of buttons
"Ja" ); //the title of the default button
// check true or false buttons pressed
if(result == 0 || result == 1) validResponse = true;
else commentArray[1] = "unzul?ssige Auswahl: W?hlen Sie einen der beiden Schaltfl?chen.";
}
return (result == 0);
} /* End of getBoolean mehod */
/**
* Eingabe vom Typ boolean
* @param prompt Hinweistext als String
* @return boolaen Eingabe als Wert vom Typ boolean
*/
public static boolean getBoolean(String prompt)
{
return getBoolean(prompt, TRUE, FALSE);
} /* end of overloaded getBoolean method */
/* 10. A ECC-validate and triming/removing function */
private static String convertfrac (String str1)
{
hold = "0" ;
char t = ' ';
str1 = str1.trim() ;
i = str1.length() ;
int count = 0 ;
for ( j = 0 ; j < i ; j++ )
{
t = str1.charAt(j) ;
if ( t == '.' ) count++ ;
if ( ( Character.isDigit(t) || ( t == '.') ) && ( count <= 1) ) hold = hold + t ;
}
return hold ;
} /* End of convertfrac method */
/* 11. Another ECC-validate and triming/removing function */
private static String convertwhole (String str1)
{
str1 = str1.trim() ;
i = str1.length() ;
hold = "0" ;
char t = ' ';
for ( j = 0 ; j < i ; j++ )
{
t = str1.charAt(j) ;
if ( Character.isDigit(t) ) hold = hold + t ;
else if ( t == '.' ) return hold ;
} return hold ;
} /* End of convertwhole method */
/**
* Festlegung des ECC-Mode
* @param get (true or false) Standard: false
*/
public static void ecc( boolean get )
{ checkecc = get ; }
/**
* Ausgabe einer Zeichenkette
* @param prompt Hinweistext als String
*/
public static void show( String prompt )
{ JOptionPane.showMessageDialog(null, prompt) ; }
/**
* Ausgabe einer Zeichenkette
* @param prompt Hinweistext als String
* @param heading Titel des Fensters als String
*/
public static void show( String prompt, String heading )
{ JOptionPane.showMessageDialog(null, prompt, heading, JOptionPane.INFORMATION_MESSAGE) ; }
} /* End of IO class */