为什么我得到java.lang.StringIndexOutOfBoundsException?

我想编写一个程序,逐步打印单词,直到出现完整的句子。 例如:我需要编写(输入)和输出:

一世
我需要
我需要
我需要写。

这是我的代码:

public static void main(String[] args) { String sentence = "I need to write."; int len = sentence.length(); int numSpace=0; System.out.println(sentence); System.out.println(len); for(int k=0; k<len; k++){ if(sentence.charAt(k)!='\t') continue; numSpace++; } System.out.println("Found "+numSpace +"\t in the string."); int n=1; for (int m = 1; m <=3; m++) { n=sentence.indexOf('\t',n-1); System.out.println("ligne"+m+sentence.substring(0, n)); } } 

这就是我得到的:

我需要写。
16
在字符串中找到0。
线程“main”中的exceptionjava.lang.StringIndexOutOfBoundsException:
字符串索引超出范围:-1 at
java.lang.String.substring(String.java:1937)at
split1.Split1.main(Split1.java:36)Java结果:1构建成功
(总时间:0秒)

我不明白为什么numSpace不计算空格的出现次数,也不知道为什么我没有得到正确的输出(即使我将numSpace替换为3)。

  1. 你没有\t字符,所以indexOf(..)返回-1
  2. 您尝试从0到-1的子字符串 – 失败

解决方案是检查:

 if (n > -1) { System.out.prinltn(...); } 

你的循环寻找numSpace是不正确的。 您正在寻找一个\t ,它是一个制表符,其中字符串中没有。

此外,当你在底部循环时,你会得到一个exception,因为你试图通过相同的\t解析,这将再次没有返回任何结果。 n的值n=sentence.indexOf('\t',n-1); 将返回-1 ,这意味着“没有你正在寻找的最后一个索引”。 然后,您尝试获取值为-1的实际子字符串,这是一个无效的子字符串,因此您将获得exception。

您错误的是\t的概念,它是水平制表符的转义序列,而不是空格字符(空格)。 搜索' '可以解决问题并找到句子中的空格。

这看起来像家庭作业,所以我的答案是一个提示。

提示:读取String.indexOf的javadoc,注意它在未找到字符串/字符时返回的值。

(事实上​​ – 即使这不是正式的家庭作业,你显然是一个Java初学者。初学者需要知道使用不熟悉的方法时javadocs是第一个看的地方。)

解决这个问题的最简单方法我想首先使用String.split函数拆分String。 像这样的东西:

 static void sentence(String snt) { String[] split = snt.split(" "); for (int i = 0; i < split.length; i++) { for (int j = 0; j <= i; j++) { if (i == 1 && j == 0) System.out.print(split[j]); else System.out.printf(" %s", split[j]); } } } 

正如其他人所指出的那样。 您将除标签(\ t)以外的每个字符计为空格。 您需要检查空格

 if (sentence.charAt(k) == ' ') 
  1. \t代表一个标签 。 要寻找空间 ,只需使用' '
  2. .indexOf()如果在字符串中找不到字符,则返回-1。 所以我们继续循环,直到.indexOf()返回-1。
  3. 这里不需要使用continue 。 当我们遇到空格时,我们增加numSpaces
  4. 当我们想要混合文字字符串和变量时, System.out.format非常有用。 不需要丑陋的+ s。

 String sentence = "I need to write."; int len = sentence.length(); int numSpace = 0; for (int k = 0; k < len; k++) { if (sentence.charAt(k) == ' ') { numSpace++; } } System.out.format("Found %s in the string.\n", numSpace); int index = sentence.indexOf(' '); while(index > -1) { System.out.println(sentence.substring(0, index)); index = sentence.indexOf(' ', index + 1); } System.out.println(sentence); } 

试试这个,它应该做你想要的。 我想你已经完成了这个,所以我只是让代码真正快速。 阅读评论,了解代码背后的原因。

 public static void main(String[] args) { String sentence = "I need to write."; int len = sentence.length(); String[] broken = sentence.split(" "); //Doing this instead of the counting of characters is just easier... /* * The split method makes it where it populates the array based on either side of a " " * (blank space) so at the array index of 0 would be 'I' at 1 would be "need", etc. */ boolean done = false; int n = 0; while (!done) { // While done is false do the below for (int i = 0; i <= n; i++) { //This prints out the below however many times the count of 'n' is. /* * The reason behind this is so that it will print just 'I' the first time when * 'n' is 0 (because it only prints once starting at 0, which is 'I') but when 'n' is * 1 it goes through twice making it print 2 times ('I' then 'need") and so on and so * forth. */ System.out.print(broken[i] + " "); } System.out.println(); // Since the above method is a print this puts an '\n' (enter) moving the next prints on the next line n++; //Makes 'n' go up so that it is larger for the next go around if (n == broken.length) { //the '.length' portion says how many indexes there are in the array broken /* If you don't have this then the 'while' will go on forever. basically when 'n' hits * the same number as the amount of words in the array it stops printing. */ done = true; } } }