2009-10-15 29 views
2

這個節目,我正在做一個COSC當然不是編譯吧,我不斷收到錯誤:螺紋字符串索引超出範圍? (Java中,子環)

異常「主要」 java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:2

在java.lang.String.substring(String.java:1765) 在VowelCount.main(VowelCount.java:13)

這裏是我的代碼:

import java.util.Scanner; 

public class VowelCount { 
public static void main(String[] args) { 
    int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0; 
    String input, letter; 
    Scanner scan = new Scanner (System.in); 

    System.out.println ("Please enter a string: "); 
    input = scan.nextLine(); 

    while (count <= input.length()) { 
    letter = input.substring(count, (count + 1)); 

    if (letter == "a") { 
    a++; } 
    if (letter == "e") { 
    e++; } 
    if (letter == "i") { 
    i++; } 
    if (letter == "o") { 
    o++; } 
    if (letter == "u") { 
    u++; } 

    count++; 

    } 
    System.out.println ("There are " + a + " a's."); 
    System.out.println ("There are " + e + " e's."); 
    System.out.println ("There are " + i + " i's."); 
    System.out.println ("There are " + o + " o's."); 
    System.out.println ("There are " + u + " u's."); 
} 
} 

要我的知識邊緣這應該工作,但爲什麼不呢?任何幫助都會很棒。謝謝!

回答

5

您可能需要取出=該行

while (count <= input.length()) { 

,並使其

while (count < input.length()) { 

,因爲它是造成子讀取超出字符串的長度。

=============== 但我要補充意見的,即使它不是問了幾個額外的比特:

不使用==來比較字符串使用

letter.equals("a") 

代替。甚至更好,嘗試使用

char c = input.charAt(count); 

,以獲得當前字符則比較像這樣:

c == 'a' 
0

卸下等號應該解決這個問題。

while (count < input.length()) {

既然你想獲得一個字符,你應該這樣做:

substr(count,1)

,因爲第二個參數實際上是長度,而不是指數。

+0

好吧,它編譯!但仍然沒有輸出正確的金額。測試字符串「aeiou」結果爲0,0,0,0,0 .. – Brad 2009-10-15 01:39:21

+0

更改爲'substr(count,1)' – mauris 2009-10-15 01:42:03

+0

不要使用==來比較字符串,如其他答案 – 2009-10-15 01:42:59

0

我覺得你的循環條件應該是count < input.length。眼下,最後一次迭代與count == length運行,所以你的substring呼叫字符串,這是違法的最後一個字符之後給出一個開始索引。寫這樣的循環時,這些類型的邊界錯誤是很常見的,所以這是一件好事,雙精度和三重檢查您的循環條件下,如果遇到這樣的錯誤。

此外,在==操作比較字符串通常不會做你想要的。比較兩個變量是否引用同一個對象。相反,你要測試string1.equals(string2),它比較兩個字符串的內容。

0

與大家的幫助,特別是文森特固定它。謝謝!運行非常好。

import java.util.Scanner; 

public class VowelCount { 
    public static void main(String[] args) { 
     int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0; 
     String input; 
     char letter; 

     Scanner scan = new Scanner (System.in); 

     System.out.print ("Please enter a string: "); 
     input = scan.nextLine(); 

     while (count < input.length()) { 
      letter = input.charAt (count); 

      if (letter == 'a') 
       a++; 
      if (letter == 'e') 
       e++; 
      if (letter == 'i') 
       i++; 
      if (letter == 'o') 
       o++; 
      if (letter == 'u') 
       u++; 

      count++; 

     } 
     System.out.println ("There are " + a + " a's."); 
     System.out.println ("There are " + e + " e's."); 
     System.out.println ("There are " + i + " i's."); 
     System.out.println ("There are " + o + " o's."); 
     System.out.println ("There are " + u + " u's."); 
    } 
} 
+0

中提到的那樣,您可以使用for循環以進一步整理它: - for(int count = 0; count pstanton 2009-10-15 04:03:01

+0

這不會提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你總是可以評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你會能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment)。 – WilQu 2014-05-30 14:04:47

+0

@WilQu你讀過這個問題嗎?這是發佈工作解決方案的OP。這應該如何評論? – meda 2014-05-30 14:08:32

0

循環之前,請嘗試以下

if(input.length()>0){ 
//you code 
} 
相關問題