Loading...
Loading...
Loading...
A shop form has a box for the quantity of items. People type all sorts of things into it. Read one line, remove the spaces at both ends, and try to convert it to an int with Integer.parseInt.
If it works, print Valid: and the number. If parseInt throws a NumberFormatException, print Invalid: and the trimmed text.
Valid: <number> or Invalid: <text>.intExample 1
Input
42
Output
Valid: 42
Example 2
Input
4two
Output
Invalid: 4two
Letters make parseInt throw NumberFormatException.
Try the problem first. Open one hint at a time.
Read the whole line with nextLine() and call trim() on it.
Put Integer.parseInt inside try, and print the invalid message in catch (NumberFormatException e).
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor