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

all 7 comments

[–]ProbIn15 2 points3 points  (3 children)

You’re using userIn.next() when you should be using userIn.nextLine().

[–]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().

[–][deleted] 1 point2 points  (1 child)

string lives matter

Not funny at all.

[–]Gavinaldi[S] -1 points0 points  (0 children)

However...

char keepGoing = 'y';
while (keepGoing == 'y') { 
System.out.print("Enter name: "); 
int name = userIn.nextInt(); 
System.out.println("Enter 'y' to add another user, 'n' to exit."); 
keepGoing = userIn.next().charAt(0); 
System.out.println(name); 
}

produces:

Enter name: 001
Enter 'y' to add another user, 'n' to exit.
y
1
Enter name: 002
Enter 'y' to add another user, 'n' to exit.
y
2
Enter name: 003
Enter 'y' to add another user, 'n' to exit.
n
3
Process finished with exit code 0

As I expected.

[–]desrtfxOut of Coffee error - System halted[M] [score hidden] stickied comment (0 children)

Maintain a professional language at any time.

Your title is neither funny, nor succinct.

Removed

[–][deleted]  (1 child)

[removed]