• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Index out of Bound

 
Ranch Hand
Posts: 81
IntelliJ IDE Oracle C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 81
IntelliJ IDE Oracle C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 81
IntelliJ IDE Oracle C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works now. Thanks to everyone.

Sen.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic