Reputation: 13
I can't find an answer about this exception. THe program is very simple but, i don't know why is incorrect.
/**
* Program selects the first word from the sentence "Hello, my dear!" with
* the String Classes's methods this.indexOf() and this.substring()
*
*
* Last modify: 29th October 2015
*/
public class TakeSubstringWanted {
public static void main(String[] args) {
String sentence = "Hello, my dear!";
String reference = "Hello";
System.out.println("The sentence is: " + sentence);
System.out.println("You want to take the first word of that sentence");
System.out.println("Give me a second...");
int firstReference = sentence.indexOf(reference);
int firstLength = reference.length();
System.out.println("The first reference of the searched word is " + firstReference);
System.out.println("The first word of the sentence " + sentence + " is: " + "\"" + sentence.substring(firstReference, firstLength) + "\"");
int secondReference = sentence.indexOf("my");
System.out.println("The second reference of the searched word is " + secondReference);
int secondLength = "my".length();
System.out.println(secondLength);
System.out.println(sentence);
System.out.println("The second word of the sentence " + sentence + " is: " + "\"" + sentence.substring(secondReference, secondLength) + "\"");
}
}
Upvotes: 0
Views: 349
Reputation: 649
Instead of giving the length, you have to give the end index. The substring method works like:
substring(int beginIndex, int endIndex)
Change
int firstLength = reference.length();
int secondLength = "my".length();
to
int firstLength = sentence.indexOf(",");
int secondLength = sentence.indexOf(" dear");
Try this:
String sentence = "Hello, my dear!";
String reference = "Hello";
System.out.println("The sentence is: " + sentence);
System.out.println("You want to take the first word of that sentence");
System.out.println("Give me a second...");
int firstReference = sentence.indexOf(reference);
int firstLength = sentence.indexOf(",");
System.out.println("The first reference of the searched word is " + firstReference);
System.out.println("The first word of the sentence " + sentence + " is: " + "\"" + sentence.substring(firstReference, firstLength) + "\"");
int secondReference = sentence.indexOf("my");
System.out.println("The second reference of the searched word is " + secondReference);
int secondLength = sentence.indexOf(" dear");
System.out.println(secondLength);
System.out.println(sentence);
System.out.println("The second word of the sentence " + sentence + " is: " + "\"" + sentence.substring(secondReference, secondLength) + "\"");
Upvotes: -1
Reputation: 1
when you are using sentence.substring() for second time the second parameter of substring method should be greater than first parameter..in your case the first parameter is 7 and second parameter is 2 ...thats why you are getting String index out of range: -5
Upvotes: 0
Reputation: 140534
The second parameter of substring is the end index, not the number of characters to take.
sentence.substring(secondReference, secondLength)
When you run this, secondLength is 2 (the length of "my"), but secondReference is 7, greater than 2, resulting in such an exception.
If you want those two characters, you'd need to use:
sentence.substring(secondReference, secondLength+secondReference)
Upvotes: 0
Reputation: 1651
Your Problem is here:
sentence.substring(secondReference, secondLength)
Your giving to lengths but it should be
sentence.substring(startIndex, endIndex);
Hope that helps!
Upvotes: 0
Reputation: 3688
Your problem is in the last line:
System.out.println("The second word of the sentence " + sentence + " is: "
+ "\"" + sentence.substring(secondReference, secondLength) + "\"");
because your secondReference = 7
and secondLength = 2
, however if you look at the documentation for substring
method:
public String substring(int beginIndex, int endIndex)
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.
...
Throws: IndexOutOfBoundsException - if beginIndex or endIndex are negative, if endIndex is greater than length(), or if beginIndex is greater than startIndex
Meaning, you're asking substring to return a string from index 7 to index 2, which causes an IndexOutOfBoundsException
. What you probably meant to do was:
sentence.substring(secondReference, secondReference + secondLength)
Upvotes: 3
Reputation: 2100
your last substring starts at pos 7 and the end index is 2, which is not possible. The last index is the end index, not the length of the char you like to take
sentence.substring(secondReference, seconfReference+secondLength);
would it be then
Upvotes: 0