Loading...
Loading...
A scorer types each innings as runs/wickets, like 245/7. Write a method static int runsOf(String score) throws Exception that returns the runs, or throws a checked Exception with one of these messages:
use the form runs/wickets if splitting on / does not give exactly 2 parts,runs cannot be negative,wickets must be 0 to 10.If runs or wickets is not a whole number, parseInt throws a NumberFormatException out of the method. In main, print OK: <score>, Bad number: <score> or Rejected <score>: <message> for each innings, then Valid innings: V and Total runs: T.
int; total runs fit in an intExample 1
Input
4 245/7 180/10 99/11 abc/3
Output
OK: 245/7 OK: 180/10 Rejected 99/11: wickets must be 0 to 10 Bad number: abc/3 Valid innings: 2 Total runs: 425
Example 2
Input
2 310-4 -5/2
Output
Rejected 310-4: use the form runs/wickets Rejected -5/2: runs cannot be negative Valid innings: 0 Total runs: 0
Try the problem first. Open one hint at a time.
Use score.split("/") and check that the array has length 2 before reading its parts.
throw new Exception("...") is a checked exception, so the method header needs throws Exception.
In main, catch NumberFormatException first and then Exception, and print e.getMessage() in the second.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor