## Number Parsing

No. 172

### Why using String userInput = "dummyValue"?

 Q: The former listing initializes the userInput variable using a default value "dummyValue". Why is that? The variable userInput is being assigned inside the try {...} block before being used in any assignment. The Scanner.nextLine() method does not throw a NumberFormatException. Thus the only way to enter the catch {...} clause is by executing of Integer.parseInt(userInput) yielding a NumberFormatException . But this execution happens after assigning userInput = scanner.nextLine(). So the dummy assignment String userInput = "dummyValue" seems to be redundant. Is that true? A: Actually the Java™ compiler is not clever enough analyzing the try {...} block. It cannot detect the impossibility of an uninitialized userInput variable inside the catch {...} clause. Since the possibility of am uninitialized userInput variable using just String userInput; causes a compile time error Variable 'userInput' might not have been initialized. Thus String userInput = null would be appropriate as well since the value will actually never be used.

No. 173

### Parsing short values

 Q: Which outcome do you expect? Try to answer without prior execution. System.out.println(Short.parseShort("32767")); System.out.println(Short.parseShort("32768"));Explain the result. A: MAX_VALUE for primitive type short is $2 16 - 1 - 1$ which equals 32767. Thus 32786 is no longer assignment compatible. So the first line will execute well but the second one yields a NumberFormatException at runtime : Value:32767 Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"32768" Radix:10 at java.lang.Short.parseShort(Short.java:120) at java.lang.Short.parseShort(Short.java:144) at de.hdm_stuttgart.sd1.ParseShortTooBig.main(ParseShortTooBig.java:8)

No. 174

### Parsing short values in hexadecimal representation

Q:

An image processing application yields the following type of data:

String[] values = {"C9", "AF3", "B"}; // Hexadecimal string values

We are looking for a class method parsing these hex values and converting them to short:

/**
* Convert a numerical value given as hexadecimal string  to
* its counterpart of type short. Example:
*
* "2B" equals 2 * 16 + 11 == 43
*
* @param input String containing hexadecimal value.
* @return the parsed short value.
*/
public static short getHexadecimal(final String input) {
...
return ...;
}

The provided sample data may then be processed like:

final String[] values = {"C9", "AF3", "B"};

for (final String value: values) {
System.out.println(value + ": " + getHexadecimal(value));
}

Result:

C9: 201
AF3: 2803
B: 11

### Tip

Have a look to the binary parse example and think about representations.

A:

Hexadecimal representation uses a radix value of 16 to be passed to short parseShort​(String s, int radix):

/**
* Convert a numerical value given as hexadecimal string  to
* its counterpart of type short. Example:
*
* "2B" equals 2 * 16 + 11 == 43
*
* @param input String containing hexadecimal value.
* @return the parsed short value.
*/
public static short getHexadecimal(final String input) {
return Short.parseShort(input, 16);
}

A:

MAX_VALUE for primitive type short is $2 16 - 1 - 1$ which equals 32767. Thus 32786 is no longer assignment compatible. So the first line will execute well but the second one yields a NumberFormatException at runtime :

Value:32767
at de.hdm_stuttgart.sd1.ParseShortTooBig.main(ParseShortTooBig.java:8)