How to avoid StringIndexOutOfBoundsException exception when char doesn't matter?

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.

+7
source share
5 answers

You can use the do-while loop. Just replace

 response = input.nextLine().charAt(0); 

from

 String line; do { line = input.nextLine(); } while (line.length() < 1); response = line.charAt(0); 

This will continue to call input.nextLine() as many times as the user enters an empty string, but as soon as they enter a non-empty string, it will continue and set response equal to the first character of this non-empty string. If you want to re-request the user for a response, you can add an invitation to the inside of the loop. If you want to verify that the user has entered the letter ad, you can also add this logic to the loop state.

+6
source

Either throw an exception ( StringIndexOutOfBoundsException ) or break this statement

  response = input.nextLine().charAt(0); 

but

  String line = input.nextLine(); if(line.length()>0){ response = line.charAt(0); } 

Exception Handling:

  try{ response = input.nextLine().charAt(0); }catch(StringIndexOutOfBoundsException siobe){ System.out.println("invalid input"); } 
+3
source

Plain:

  • First enter the source text as String and put it in the String temporary variable.
  • Then check the length of the string.
  • then if> 0, extract the first char and use it.
+2
source

Also, the answer is @HovercraftFullOfEels (completely correct), I would like to point out that you can β€œcatch” these exceptions. For example:

 try { response = input.nextLine().charAt(0); } catch (StringIndexOutOfBoundsException e) { System.out.println("You didn't enter a valid input!"); // or do anything else to hander invalid input } 

i.e. if a StringIndexOutOfBoundsException is encountered when executing a try block, the code in the catch block will be executed. You can learn more about catches and exception handling here .

+2
source

StringIndexOutofBoundException also occurs in the following situation.

  • Search for a string that is not available
  • Match string that is not available

    for ex:

    List ans = new ArrayList ();
    Pace = "and";
    String arr [] = {"android", "jellybean", "kitkat", "ax"}; for (int index = 0; index <arr.length; index ++)
    if (temp.length () <= arr [index] .length) ()
    if (temp.equlsIgnoreCase ((String) arr ['index] .subSequence (0, temp.length ())));
    ans.add (arr [index]);

the following code is required to avoid indexoutofbound exception

if (temp.length () <= arr [index] .length ())

because here we say that the length of the src string is equal to or greater than temp. if src string length is less than through "arrayindexoutof boundexception"

0
source

All Articles