我无法找到关于此例外的答案。该程序非常简单,但我不知道为什么不正确。
/**
* 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) + "\"");
}
}
答案 0 :(得分:3)
你的问题在最后一行:
System.out.println("The second word of the sentence " + sentence + " is: "
+ "\"" + sentence.substring(secondReference, secondLength) + "\"");
因为您的secondReference = 7
和secondLength = 2
,但是如果您查看substring
IndexOutOfBoundsException
方法{/ p>}:
public String substring(int beginIndex, int endIndex)
返回一个新字符串,该字符串是此字符串的子字符串。子字符串从指定的beginIndex开始,并扩展到索引endIndex - 1处的字符。因此子字符串的长度为endIndex-beginIndex。
...
<强>抛出:强> IndexOutOfBoundsException - 如果beginIndex或endIndex为负数,如果endIndex大于length(),或者beginIndex大于startIndex
意思是,您要求substring将索引7中的字符串返回到索引2,这会导致sentence.substring(secondReference, secondReference + secondLength)
。你可能想做的是:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
`
<include layout="@layout/toolbar"/>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
</android.support.v4.widget.DrawerLayout>
答案 1 :(得分:0)
substring的第二个参数是结束索引,而不是要采用的字符数。
sentence.substring(secondReference, secondLength)
当你运行它时,secondLength是2(“my”的长度),但secondReference是7,大于2,导致这样的异常。
如果你想要这两个字符,你需要使用:
sentence.substring(secondReference, secondLength+secondReference)
答案 2 :(得分:0)
你的最后一个子串从pos 7开始,结束索引是2,这是不可能的。最后一个索引是结束索引,而不是你想要的char的长度
sentence.substring(secondReference, seconfReference+secondLength);
那会是吗?
答案 3 :(得分:0)
你的问题在这里:
sentence.substring(secondReference, secondLength)
你给予长度,但它应该是
sentence.substring(startIndex, endIndex);
希望有所帮助!
答案 4 :(得分:0)
当您第二次使用 sentence.substring()时,substring方法的第二个参数应该大于第一个参数..在您的情况下,第一个参数是7,第二个参数是2。 ..为什么你得到的字符串索引超出范围:-5
答案 5 :(得分:-1)
您必须提供结束索引,而不是给出长度。 substring方法的工作方式如下:
substring(int beginIndex, int endIndex)
更改
int firstLength = reference.length();
int secondLength = "my".length();
到
int firstLength = sentence.indexOf(",");
int secondLength = sentence.indexOf(" dear");
试试这个:
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) + "\"");