Loading...
Loading...
Loading...
A payment form must read amounts typed by people, without using Integer.parseInt(). Follow these rules exactly:
+ or - sign may come next.+, - and . onlyExample 1
Input
-42
Output
-42
Leading spaces are skipped, the minus sign is kept.
Example 2
Input
4193 with words
Output
4193
Reading stops at the space after 4193.
Try the problem first. Open one hint at a time.
Use an index i. Move it past spaces, then check for one + or -.
For each digit, value = value * 10 + (ch - '0'), where ch - '0' turns a digit character into its number.
Keep the value in a long and stop as soon as it passes 2147483648, so even 20+ digits cannot overflow the long.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor