2011-09-19 85 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; 
    } 
    } 
} 
相關問題