Loading...
Loading...
A word game shows a word and asks the player for a position, counting from 1. Print the letter at that position.
Two things can go wrong. If the position is not a whole number, print Position must be a whole number. If it is outside the word, charAt throws a StringIndexOutOfBoundsException; print Position out of range. Use one catch block for each problem.
Letter: <c>, Position must be a whole number or Position out of range.int between -1000 and 1000Example 1
Input
MENTOR 3
Output
Letter: N
Position 3 is index 2.
Example 2
Input
JAVA 7
Output
Position out of range
JAVA has only 4 letters.
Try the problem first. Open one hint at a time.
Read both lines as text. Convert the position with Integer.parseInt inside the try block.
The letter is word.charAt(pos - 1).
Write two catch blocks: NumberFormatException and StringIndexOutOfBoundsException.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor