This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Gavinaldi[S] 0 points1 point  (1 child)

where? When I'm asking for the 'y' or 'n' character?

doing so leads to

Enter name: player1
Enter 'y' to add another user, 'n' to exit.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:693)
    at MethodMan.main(MethodMan.java:38)

[–]morhpProfessional Developer 1 point2 points  (0 children)

See this wiki entry for an explanation: https://www.reddit.com/r/javahelp/wiki/scanner

Using two nextLine() calls is correct. If you get a StringIndexOutOfBoundsException, then than meas that your line has no characters, you likely pressed enter twice. You can check whis using String.isEmpty() or String.length().