Loading...
Loading...
Loading...
A hostel mess writes bills as plain text, like Paid 250 for lunch and 40 for tea. Read the text, pick out every number (a run of digits), and print them in order and their sum. A number with leading zeros, like 007, is printed as its value, 7.
None if there are no numbers. The second line is Sum: s.Example 1
Input
Paid 250 for lunch and 40 for tea
Output
250 40 Sum: 290
Example 2
Input
a1b2c3
Output
1 2 3 Sum: 6
Numbers can touch letters.
Try the problem first. Open one hint at a time.
Walk through the text and collect digits into a StringBuilder while you see them.
When a non-digit comes (or the text ends) and the builder is not empty, the number is complete: parse it and clear the builder.
Use long for the sum.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor