2011-09-19 104 views
0

我想编写一个程序,逐步打印单词直到出现一个完整的句子。例如:我需要编写(输入),输出:为什么我会得到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)); 
    } 
} 

,这就是我得到:

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

我不明白为什么numSpace不计空格的发生,也不知道为什么我不得到正确的输出(即使我将numSpace替换为3)。

+0

看起来你指望标签'“\ t'',而不是空格'”“' – Sam

回答

0

解决这个问题的最简单的方法是我想通过使用函数String.split首先分割字符串。像这样的:

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
  1. 检查空格,则没有\t角色,所以indexOf(..)回报-1
  2. 您尝试从0到子-1 - 失败

的解决方案是检查:

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

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

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

+0

非常感谢你!我不知道,\ t是一个制表符,而不是一个字符串! – Doodi

1

你被\t的概念误解了,它是水平制表符的转义序列,而不是空白字符(空格)。搜索' '会做的伎俩,并找到你的句子中的空格。

1

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

提示:请阅读javadoc的String.indexOf关注它对字符串/字符未找到时返回的值的说明。

(其实 - 即使这不是正式的功课,你都清楚一个Java初学者,初学者需要了解的是,javadocs使用不熟悉的方法时,首先关注的地方。)

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

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); 
} 
0

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

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; 
    } 
    } 
}