Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: java.lang.StringIndexOutOfBoundsException: String index out of range: 0

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    I am having trouble with my program. I finished it but, I can not get the begging to cooperate. I need to take a users input if they would like to play a game. I have to use the charAt method to take the first character of their input and if it is a N or n then the program ends. Otherwise it continues. It works but, if the enter key is pressed then the program crashes and I receive the Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:687)
    at Ex2.main(Ex2.java:21)

    Can anyone help me correct this? I have to us charAt
    Here is my code (just the part where I am having issues)
          String input;
          char answer;
     
     
           System.out.print("Would you like to play the guessing game? ");
           input = keys.nextLine();       
           answer = input.charAt(0);
     
             if(answer == 'n'|| answer =='N')
             {
              System.exit(0);
             }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    The String class has a method, length(), that tells you how long the String is. Consider checking this first before trying to extract a character that doesn't exist.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    So if I added something like this to check the length of the string, where would I need to add it to prevent the crash? Would I had a comparison to my if statement?
    int size;
    size = input.length();

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    Quote Originally Posted by KristopherP View Post
    So if I added something like this to check the length of the string, where would I need to add it to prevent the crash?
    // your code goes here
    int size;
    size = input.length();
    You would need to use an if block, similar to what you're doing above, and it would make most sense to check for the size immediately before the error, putting your code where you extract your first character inside the if block.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    I tried your suggestion with
     if( size > 0)
             {
               answer = input.charAt(0);
             }

    When I get to the next part it tells me answer is not initialized
     if(answer == 'n'|| answer =='N')
             {
              System.exit(0);
             }

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    Quote Originally Posted by KristopherP View Post
    I tried your suggestion with
     if( size > 0)
             {
               answer = input.charAt(0);
             }

    When I get to the next part it tells me answer is not initialized
     if(answer == 'n'|| answer =='N')
             {
              System.exit(0);
             }
    You only want to get to the "next part" if the first if is true. Consider nesting your if blocks, putting the if (answer == 'n'... bit inside the if (size > 0) bit.

  7. The Following User Says Thank You to curmudgeon For This Useful Post:

    KristopherP (April 22nd, 2013)

  8. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    I did the nested if and it worked. Thanks for your help

Similar Threads

  1. String Index Out of range
    By Shikha Jain in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 09:14 PM
  2. String index is out of range
    By etag in forum Exceptions
    Replies: 9
    Last Post: April 4th, 2012, 10:59 PM
  3. String index out of range?
    By oksmartypants in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2012, 01:10 PM
  4. String Index out of range
    By petemyster in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 02:59 PM
  5. Replies: 2
    Last Post: November 3rd, 2009, 05:28 AM