String Indexoutofbounds
whats wrong?
publicstatic String capitalize(String s){
char ch;
if (s.length() == 0)// Special case: empty string
return s;
StringBuffer result =new StringBuffer();
for(int i=0; i<s.length(); i++){
ch = s.charAt(i);
if(Character.isLetter(ch) && !Character.isLetter(s.charAt(i-1)))
result.append(Character.toUpperCase(ch));
else
result.append(Character.toLowerCase(ch));
}
return result.toString();
}
When i run this method i get
xception in thread"main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
But i dont see if the string is out of bounds?>
You start you index 'i' at 0 and then you access character s.charAt(i-1). Do you have a character at index -1?
> You start you index 'i' at 0 and then you access
> character s.charAt(i-1). Do you have a character at
> index -1?
yes i am passing this string "knight rider" now i would like to capitalize the first letter. my problem was the spacing. i thought to have !Character.isLetter(s.charAt(i-1)) which will check if the preceeding char is the first letter if not then charAt(i-1). am i wrong?
> > You start you index 'i' at 0 and then you access
> > character s.charAt(i-1). Do you have a character
> at
> > index -1?
>
> yes i am passing this string "knight rider" now i
> would like to capitalize the first letter. my problem
> was the spacing. i thought to have
> !Character.isLetter(s.charAt(i-1)) which will check
> if the preceeding char is the first letter if not
> then charAt(i-1). am i wrong?
Which character precedes the first character of your string?
> Which character precedes the first character of your> string?hope i am understanding you right. i would like to capitalize k and r. the frst character of each string. k preceeds the first character.
> > Which character precedes the first character of
> your
> > string?
>
> hope i am understanding you right. i would like to
> capitalize k and r. the frst character of each
> string. k preceeds the first character.
I don't care what algorithm you are implementing, you are actually trying to access the character before the first character of your string. THERE IS NO CHARACTER BEFORE THE FIRST CHARACTER OF YOUR STRING - THAT IS WHY IT IS THE FIRST CHARACTER OF YOUR STRING.
but how ?System.out.println(capitalize("knight rider"));i dont see any first empty character?
One last try - when i = 0"knight rider".charAt(i) is 'k'but what is "knight rider".charAt(i-1) ?
> One last try - when i = 0> "knight rider".charAt(i) is 'k'> but what is > "knight rider".charAt(i-1) ?it means there is no preceding character. if the character is at s.charAt(i) then preceeding character will be s.charAt(i-1).
> > One last try - when i = 0
> > "knight rider".charAt(i) is 'k'
> > but what is
> > "knight rider".charAt(i-1) ?
>
> it means there is no preceding character. if the
> character is at s.charAt(i) then preceeding character
> will be s.charAt(i-1).
You are still missing the point - the valid index range (bounds) for s.charAt(index) is from 0 to (s.length() -1) so index (i-1) is 'out of bounds' when i is zero. Hence the exception.
hmmm ok that makes sense. then what do you suggest to do? like how can i make an algorithm to tell that if there is a leading space in the string like "knight rider" then capitalize the first letter after the space also? i tried with an if statement something like this
private static final String SPACE = " ";
//and then if statement but did not work
if(Character.isLetter(ch) && ((i == 0) || !Character.isLetter(s.charAt(i-1))))Edit - silly me - has to be the shortcut 'or' operator ||Message was edited by: sabre150
Wow that worked thanks Miss Sabre. Just curious like thats a way big if statement. i mean the logic is right and perfect. Let me see if i can find another way to do it also without that big if statement. Thanks Alot
> Wow that worked thanks Miss Sabre. That hurts! What made you decide I was a 'Miss' ?
oops my bad lol Mr sabre thanks alot for your help..