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

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    String Index out of Bounds Exception error...again


    I've had this error before on different programs, but I thought I'd worked out what wasn't working and my recent programs have done this same thing just fine.

    Code:
    Char1 = 
    reverseWord.charAt(length1-1); 					
    while (length1>0){ 						
    String characters = ""; 						
    if (!(Char1 == 'a'|| Char1 == 'e'|| Char1 == 'i'|| Char1 == 'o'|| Char1 == 'u'|| Char1 == 'A'|| Char1 == 'E'|| Char1 == 'I'|| Char1 == 'O'|| Char1 == 'U')){ 							
    characters =Character.toString(Char1)+characters; 						
    } 						
    if (length1>0){ 						
    reverseWord = reverseWord.substring(0,length1-1); 						
    length1 = 
    reverseWord.length(); 						
    if (length1>0){ 							
    Char1 = 
    reverseWord.charAt(length1-1); 						
    } 						
    } 						
    reverseWord = characters; 					
    }
    reverseWord and length1 were already created. the point is to remove the vowels. The error I'm getting is: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2.
    I can't work out how I'm getting this error because, as I already mentioned, I have used essentially the same code many times before without getting the error. Please explain.

    (the code formatting came out weird, but that's not the problem. The format is fine in my actual code)
  2. #2
  3. Recovering Intellectual
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2006
    Location
    Orange County, CA
    Posts
    1,306
    Rep Power
    785
    can you provide the input values for reverseWord? If the word is "to" for example you would get that error.
    Bugs that go away by themselves come back by themselves
    Beware - your loyalty will not be rewarded
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    I agree with Matt. I ran your code, and even on a one character String, there wasn't a problem. I did notice one logic problem though. You redeclare characters as a local variable in the while loop each time it executes.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    In reply to Matt, I get the error for any String I enter as reverseWord.
    In reply to bullet, thaks. I have now declared it outside but kept the = "" inside the loop.

    I also put
    Code:
    reverseWord = characters;
    outside of the while loop. So now I don't get an error. Unfortunately, it still doesn't work properly. If I enter "busy bumble bee" for reverseWord, the program output is "b b b", just the first letters of the words rather than the desired output "bsy bmbl b".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    Never mind, I fixed it. If I also take out the characters = ""; from the while loop like bullet said then it work properly. The way I had it, I only got the first letter because it was clearing the rest of the letters every time the loop repeated. My original error occurred because with I had reverseWord = characters; after I'd already re-evaluated the length of the String, so it was trying to use a length that was longer than the actual String.
    Thank you for your help.
  10. #6
  11. Recovering Intellectual
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2006
    Location
    Orange County, CA
    Posts
    1,306
    Rep Power
    785
    Next time post the whole class / method - it was difficult trying to debug half an algorithm. And take the care and time to repost your code with the appropriate code tags.

    Glad you found the solution.
    Bugs that go away by themselves come back by themselves
    Beware - your loyalty will not be rewarded

IMN logo majestic logo threadwatch logo seochat tools logo