#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Java String Index out of Bounds Exception error


    As part of a larger project, I am trying to cut down a sentence input by the user to include only lowercase letters and numbers. There should be no spaces, no capital letters and no symbols.
    Code:
    public void storeLongString(){
    		longString = longString.trim();//trims the string input by the user so no white space on either side
    		wordLength = longString.length();//stores string length in int wordLength
    		longString = longString.toLowerCase();//makes whole string lowercase
    		myChar = longString.charAt(wordLength - 1);//sets char myChar to last character in the inputted String
    		while(wordLength >=1){//this is where my problems start
    			if (myChar == 'a' || myChar == 'b' || myChar == 'c'|| myChar == 'd' || myChar == 'e' || myChar == 'f' || myChar == 'g' || myChar == 'h' || myChar == 'i' || myChar == 'j' || myChar == 'k' || myChar == 'l' || myChar == 'm' || myChar == 'n' || myChar == 'o' ||myChar == 'p' || myChar == 'q' || myChar == 'r' || myChar == 's' ||myChar == 't' || myChar == 'u' || myChar == 'v' || myChar == 'w' ||myChar == 'x' || myChar == 'y' || myChar == 'z' || myChar ==  '0' ||myChar == '1' || myChar == '2' || myChar == '3' || myChar =='4' || myChar == '5' || myChar == '6' || myChar== '7' || myChar == '8' || myChar == '9'){//although this if statement looks really awkward, it just says if myChar is a lowercase letter or a number
    				newLongString +=myChar;//newLongString will be the string that stores the simplified version of longString containing only lowercase letters and numbers
    			}
    			longString = longString.substring(1);//removes the first character from longString
    			longString = longString.trim();//removes any spaces that result from taking off the first character
    			wordLength = longString.length();//calculates the new length of the string
    			myChar = longString.charAt(wordLength-1);//this is the lone that gets the errors.
    		}
    	}
    The error specifically states:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(Unknown Source)
    Why am I getting that error? I don't see why it would ever get a negative 1 value since the while statement should stop it from executing when wordLength = 1, and therefore the lowest I would get would be zero.
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1712
    Originally Posted by Person33
    Code:
    wordLength = longString.length();//calculates the new length of the string
    myChar = longString.charAt(wordLength-1);//this is the lone that gets the errors.
    Why am I getting that error? I don't see why it would ever get a negative 1 value since the while statement should stop it from executing when wordLength = 1, and therefore the lowest I would get would be zero.
    No, the loop doesn't instantly stop if wordLength is < 1. It only stops when it gets back to the "while" statement. In the snippet of code above, what if wordLength is set to zero (that is, longString is the empty String)? What happens on the second line of the snippet above?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    Thanks. I got that part fixed. Unfortunately, now there's another issue. At the point where I'm trying to take the length of the entire string, I think it's only storing the length of the first word, because the program ends up only printing the first word in reverse and ignoring all the others. How do you get the length of the entire line?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    Never mind. I worked out what was wrong. Ignore my previous post.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    hey,

    would you mind sharing your answer?

    the bit of code where you say if(myChar == 'a'...) is long, and i remember there being a shorter way to write all that out

    edit: found it!

    if((myChar >= 'a' && myChar <= 'z') || ( myChar>=0 && myChar <=9)){
    }
  10. #6
  11. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1712
    The people who have to now make your code work for a non-US based language will hate you later

    Try to use the existing library - Character.isLowerCase() and Character.isDigit()
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    Yeah, I knew there was an easier way, but I couldn't remember it. Thanks.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    My answer? Sorry, but what exactly do you want me to share? The program output or the final fix.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by stdunbar
    The people who have to now make your code work for a non-US based language will hate you later

    Try to use the existing library - Character.isLowerCase() and Character.isDigit()
    Thanks! Yeah, much easier B)

IMN logo majestic logo threadwatch logo seochat tools logo