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 10 of 10

Thread: String index is out of range

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation String index is out of range

    Ok so I'm really novice at java and have been struggling with this particular exception. When I go and enter a name into the string, it pops the exception:
    License Validator
    Enter a last name: enter
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.charAt(Unknown Source)
    I could really use some help at this point I'm beat and struggling to really get anywhere with it.

    System.out.println("License Validator");
     
    		String name1;
    		int y = 1;
     
    		while(y > 0){
    			System.out.print("Enter a last name: ");
    			name1 = stdIn.nextLine();
     
    			int namelength = name1.length();
     
    			while(namelength>0){
    				char x = name1.charAt(namelength);


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: String index is out of range

    What String are you trying to access? How many indexes does it have? Which index are you trying to access?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: String index is out of range

    Heres what I'm trying to do:

    System.out.print("Enter a last name: ");
    name1 = stdIn.nextLine();

    The program outputs to the user asking for a last name then the user input is stored in the string name1 using scanner class.
    Then the line: int namelength = name1.length();. Is being used to get and store the length of the string name1 in the interger namelength.
    That is where the exception occurs and it says that the "String index is out of range."

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: String index is out of range

    Actually your error is occurring at the line
    char x = name1.charAt(namelength);

    You are probably incrementing namelength or decreasing the size of name1 somewhere, but not checking your location in the String or resetting the size (respectfully). We'll need to see more of that while loop to provide you with any useful tips.

    EDIT:
    namelength is the length of your String, but the index starts counting at 0 and ends at the length of the String minus 1. I assume you are trying to get the last character of name1. To do that, you need to say: namelength-1 instead of namelength.
    Last edited by aussiemcgr; April 2nd, 2012 at 06:20 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    etag (April 2nd, 2012)

  6. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: String index is out of range

    Thanks for the help aussie, I was looking in the wrong place.
    The while loop is a verification loop designed to ensure the name entered only contains letters or whitespace(But now two in a row, which I haven't worked out).

    while(namelength>0){
    	char x = name1.charAt(namelength);
                 if(Character.isLetter(x)){
                      namelength = namelength -1;
                 }
                 if(Character.isDigit(x)){
                      namelength = 0;
                 }
                 if(Character.isWhitespace(x)){
                     namelengtj = namelength - 1;
                 }
    {

    Thats what is in the loop, please let me know if you have any more questions.

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: String index is out of range

    Ok, you've made a very simple mistake that is very often seen in those who are new to programming. Your mistake has to do with the difference between how a human counts and how the computer counts. When humans count, we start at 1, but when computers count, they start at 0.

    So, let's say we have the word: Programming. That word has 11 characters, so its length is 11. But, if we want to get the first character, word.charAt(1); will give us r, NOT P. The reason is because indexing starts at 0 and ends at the length of the word, minus one. For example, here is the indexes of the word:
    P = 0
    r = 1
    o = 2
    g = 3
    r = 4
    a = 5
    m = 6
    m = 7
    i = 8
    n = 9
    g = 10

    So, as you can see, to get the first letter, we say: word.charAt(0);, and to get the last letter we say: word.charAt(10);. Or, to get the last letter of any word, we say: word.charAt(word.length()-1);.

    Does that make sense?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. The Following User Says Thank You to aussiemcgr For This Useful Post:

    etag (April 2nd, 2012)

  9. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: String index is out of range

    Ah I knew I was missing something very basic.

    char x = name1.charAt(namelength - 1);
    fixed the exception by adding the -1, but now the if statements that follow don't work either.

  10. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: String index is out of range

    Quote Originally Posted by etag View Post
    Ah I knew I was missing something very basic.

    char x = name1.charAt(namelength - 1);
    fixed the exception by adding the -1, but now the if statements that follow don't work either.
    What does it do instead? Does it throw an Exception? Does it exhibit some strange behavior? Does it sprout wings and fly away? Something else? Telling us that "it don't work" is as useful to us as us saying "then fix it" is useful to you.

    If you want help, please post the updated code (in SSCCE form, with highlight tags) and explain exactly what's happening.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: String index is out of range

    I assume you also want to change your while loop from:
    while(namelength>0)
    to
    while(namelength>=0)
    since the index 0 is a valid index.
    Also, check the last if statement in your loop. I think you misspelled namelength.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  12. #10
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: String index is out of range

    Sorry about the short and not to the point replies, I understand that it was pointless and didn't help me or yourself. On the other hand I did end of fixing the overall program by switching from the process I was using and just going with a pattern and matcher setup which ran a lot easier and more compact than trying to use multiple loops to verify each letter in the user inputs.

Similar Threads

  1. String.split("") puts null in the first index
    By sonicjr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 17th, 2012, 04:25 AM
  2. String index out of range?
    By oksmartypants in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2012, 02:10 PM
  3. String Index out of range
    By petemyster in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 03:59 PM
  4. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 08:11 PM
  5. set the slider's models (range)
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 28th, 2009, 11:59 PM