wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Index out of Bound Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Index out of Bound" Watch "Index out of Bound" New topic
Author

Index out of Bound

Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
The following compiles successfully.

class strtest {
public static void main (String args[]) {

String[] inputstring = {"abcdef"};
String revstring = "abcdefg";
String newstring = "";
int leng = 0;
int i = 0;


for (String str:inputstring) {
newstring = newstring + str;
}

leng = newstring.length();
System.out.println(leng);
System.out.println("New string: "+newstring);

i = leng;

// Parse string once again and assign the characters to the string variable newstring in reverse order

for (int j = leng - 1; j>=0; j--) {
revstring = revstring + newstring.substring(j,1);
System.out.println("Rev string : " + revstring);

}
}

}
-----------------------------------------------------------------------

But the following run-time error comes up.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -4
at java.lang.String.substring(String.java:1768)
at strtest.main(strtest.java:24)

The print command have verified the right values in the variables leng and newstring as 6 and 'abcdef' respectively.

Please help.

Regards,
Sen.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

Take a look at the following line of code:



The substring() method takes two parameters -- a begin index and an end index. Here is a quote in the Java Docs which discusses the requirements of the index.

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


Hope this helps,
Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
Given that, I trust that if there is a variable s defined as
String s = "abcdef";

then the operation s.substring(3,3) should return c and the operation s.substring(1,1) should return the character a.

I recompiled the the program snippet mentioned in the post with the right index bounds-with one change I reset the variable revstring to "-but am getting all blanks in the output.

Sen.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

Originally posted by Sen George:
Given that, I trust that if there is a variable s defined as
String s = "abcdef";

then the operation s.substring(3,3) should return c and the operation s.substring(1,1) should return the character a.

I recompiled the the program snippet mentioned in the post with the right index bounds-with one change I reset the variable revstring to "-but am getting all blanks in the output.


No offense... but... if you would had taken a minute to read the Java Docs, you would have save a lot of time.

Anyway, the quote from the docs that explains what the indexes are:

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.


BTW, there are also some examples there too.

Henry
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Sen George:
Given that, I trust that if there is a variable s defined as
String s = "abcdef";

then the operation s.substring(3,3) should return c and the operation s.substring(1,1) should return the character a.

I recompiled the the program snippet mentioned in the post with the right index bounds-with one change I reset the variable revstring to "-but am getting all blanks in the output.

Sen.


As pointed out above, your analysis is incorrect. The operation s.substring(n, n) will return "" (i.e. an empty String). Read the quote from the Javadocs that Henry posted above to see why.

Layne


Java API Documentation
The Java Tutorial
Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
Originally posted by Sen George:
Given that, I trust that if there is a variable s defined as
String s = "abcdef";

then the operation s.substring(3,3) should return c and the operation s.substring(1,1) should return the character a.

Sen, there is another error in your analysis that Henry and Layne did not mention, and which is also not explicitly mentioned in the docs entry for substring. Java String indexes begin at 0, not at 1, so the character at index 3 in "abcdef" is d, not c.
Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
It works now. Thanks to everyone.

Sen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Index out of Bound