1 // NumberTest.java
2 import java.util.Scanner;
3
4 /** 5 * NumberTest zeigt den Umgang mit der Klasse Number. 6 * Beispielprogramm zur Programmiertechnik 1, Teil 5. 7 * @author H.Drachenfels 8 * @version 30.5.2018 9 */
10 public final class NumberTest {
11 private NumberTest() { }
12
13 /** 14 * main ist der Startpunkt des Programms. 15 * @param args Feld mit Dezimalzahlen 16 */
17 public static void main(String[] args) {
18 for (String z : args) {
19 Scanner s = new Scanner(z);
20 Number n;
21
22 if (s.hasNextByte()) {
23 n = Byte.valueOf(s.nextByte());
24 } else if (s.hasNextShort()) {
25 n = Short.valueOf(s.nextShort());
26 } else if (s.hasNextInt()) {
27 n = Integer.valueOf(s.nextInt());
28 } else if (s.hasNextLong()) {
29 n = Long.valueOf(s.nextLong());
30 } else if (s.hasNextFloat()) {
31 if (z.length() < 9) {
32 n = Float.valueOf(s.nextFloat());
33 } else {
34 n = Double.valueOf(s.nextDouble());
35 }
36 } else {
37 System.err.printf("Not a number: %s%n", z);
38 continue;
39 }
40
41 System.out.printf("%s(%s):%n", n.getClass().getSimpleName(), n);
42 System.out.println(n.byteValue());
43 System.out.println(n.shortValue());
44 System.out.println(n.intValue());
45 System.out.println(n.longValue());
46 System.out.println(n.floatValue());
47 System.out.println(n.doubleValue());
48 }
49 }
50 }
51