aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes substring question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "substring question" Watch "substring question" New topic
Author

substring question

Piyush Sam
Greenhorn

Joined: Jun 02, 2005
Posts: 24
Hello everybody,

What will be the output of the following program:

1 System.out.println("string is: " + "emptiness".substring( 9 ));
2 System.out.println("string is: " + "emptiness".substring( 10 ));

2 should throw java.lang.StringIndexOutOfBoundsException but first one return an empty string,

Can anybody explain this why is it giving this?

Regards,


Piyush Jain<br /> <br />Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
I looked up the API and found this,


Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
Examples:

"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"

"emptiness".substring(9) returns "" (an empty string)



In your example
e m p t i n e s s
0 1 2 3 4 5 6 7 8

"emptiness".substring( 9 )

There is nothing at index 9, therefore the exception, java.lang.StringIndexOutOfBoundsException

Same applies for the next String as well
[ June 18, 2005: Message edited by: Abdulla Mamuwala ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There is nothing at index 9, therefore the exception, java.lang.StringIndexOutOfBoundsException


No. There is no exception in this case. It returns the emptystring "".

The end of the string is marked by an index of 9. What is between the end of the string and the end of the string? It's nothing. So the method returns an empty string.

"emptyness".substring(9) is the same as "emptyness".substring(9, 9). The length of the substring is 9 - 9 = 0 characters; and that is the length of the empty string "".


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Barry,



When I execute the above program I get the following output,
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1444)
at java.lang.String.substring(String.java:1411)
at MyString.main(MyString.java:18)
Exception in thread "main"

Now, when I look up the API for public String substring(int beginIndex), I also see,

IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.

In the case above surely the beginIndex is not negative, but it is for sure greater than the length of the String object.

The length of the String object is 8 ( is it here that I am making a mistake) but the value of the beginIndex which is 9 is more than that of the String object.

Now when I look at the StringIndexOutOfBounds API, I see the following defination,

Thrown by String methods to indicate that an index is either negative or greater than the size of the string.


Here again it says that the index is greater than the size of the String.

Now please could you tell me where does your explaination fit here

What is between the end of the string and the end of the string?
"emptyness".substring(9) is the same as "emptyness".substring(9, 9). The length of the substring is 9 - 9 = 0 characters; and that is the length of the empty string "".


According to your explaination what I can figure out is that because an empty String is returned , the exception is thrown, but the StringIndexOutOfBoundsException tells us that because there is some problem with the index value an exception is thrown.

Please do explain to me if my interpretation of your explaination is correct or I am completely misunderstanding what you are trying to say.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
When I execute the code "emptyness".substring(9) it returns "".

"emptyness".substring(10) is the one that throws the exception.
[ June 18, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The length of the String object is 8 ( is it here that I am making a mistake) but the value of the beginIndex which is 9 is more than that of the String object.


"emptiness" (or "emptyness" as I misspelled it) has 9 characters, not 8. The character at index 8 is the last 's'. The index 9 'points' to just after that 's'. And 9 is equal to the length of the string so it is perfectly legal to use it as the argument to substring(int).
[ June 18, 2005: Message edited by: Barry Gaunt ]
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Thanks Barry you cleared my doubt. I was all the time under the impression that because subString( int beginIndex) is starting at zero therefore the length of the string would be '8 'and as such exceed the length of the String object. But Barry, still one doubt remains in regards to emptiness.subString(10). The index surely exceeds the length of the String object here, but your explaination here,

The end of the string is marked by an index of 9. What is between the end of the string and the end of the string? It is nothing. So the method returns an empty string


which according to me implies that the exception is thrown because an emty String is returned.

Now, my doubt is, is it because of the empty String that the exception is thrown or is it because the size of the argument '10' in method subString() exceeds that of the length of the String object.
[ June 18, 2005: Message edited by: Abdulla Mamuwala ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
My only confusion is that returning an empty String " " is the real cause of the exception as you have said,


I did not say the returning the empty string "" is the cause of the exception. There is no exception in the case of using 9 as the index. I tried to explain why "" is returned in the case of "emptiness".substring(9).

In the case of 10, the exception being thrown is expected because 10 > 9 which is the length of "emptiness".
[ June 18, 2005: Message edited by: Barry Gaunt ]
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Thanks Barry got it, end of confusion.
Piyush Sam
Greenhorn

Joined: Jun 02, 2005
Posts: 24
Thanks Barry for such a wonderful explaination.

Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: substring question