Sorry if the title does not make sense, but I did not know how to write it.
Problem:
I am doing a multi-user quiz that gets either user a, b, c, or d. This is not a problem if they do as they are told, however, if they do not type anything and just fall into the box, I get a StringIndexOutOfBoundsException exception. I understand why this is happening, but I'm new to Java and can't figure out how to fix it.
What I still have:
System.out.println("Enter the Answer."); response = input.nextLine().charAt(0); if(response == 'a') { System.out.println("Correct"); } else if(response == 'b' || response == 'c' || response == 'd') { System.out.println("Wrong"); } else { System.out.println("Invalid"); }
Of course, the program will never skip the second line of code if the user does not type anything, because you cannot take the charAt (0) value of an empty String. What I'm looking for is what checks to see if the answer is null, and if so, ask him to come back and ask the user again.
Thanks in advance for any answers.
hashtag name
source share