Loading...
Loading...
Loading...
The college canteen numbers its items with tokens 1, 2, 3 and so on. Item with token t is stored at index t - 1 of a prices array. A student types a token number and the screen shows the price.
If the token does not match any item, reading the array throws an ArrayIndexOutOfBoundsException. Catch it and print No item for token t.
Price: <price> or No item for token <t>.Example 1
Input
4 30 45 20 60 2
Output
Price: 45
Token 2 is index 1.
Example 2
Input
4 30 45 20 60 5
Output
No item for token 5
Index 4 does not exist in an array of 4.
Try the problem first. Open one hint at a time.
Read the prices into an int[] with a loop, then read the token.
Inside try, print prices[t - 1]. Catch ArrayIndexOutOfBoundsException for a bad token.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor