Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Index out of Bound

 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kenneth Albertson
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works now. Thanks to everyone.

Sen.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic