2014-03-01 152 views
0

好的 - 我知道我非常接近解決方案,但需要向正確的方向微調。我需要用戶點擊「是」,程序纔會再次提問。while循環JOptionPane問題

執行後,我收到以下錯誤

異常線程 「main」 java.lang.StringIndexOutOfBoundsException: 字符串索引超出範圍:1,在java.lang.String.charAt(未知 來源)在Vowel3.main(Vowel3.java:49)

// java class for Panel I/O 
import javax.swing.JOptionPane; 

// declaration of the class 
public class Vowel33 
{ 

    // declaration of main program 
    public static void main(String[] args) 
    { 

    // objects used to store data 
    String input_string = null; 
    int a_count = 0; 
    int e_count = 0; 
    int i_count = 0; 
    int o_count = 0; 
    int u_count = 0; 
    int i = 0; 
    int yes = 0; 

    // 1. display a descriptive message 
    String display_message = "This program asks the user for a sentence,\n" 
     + "searches the sentence for all vowels,\n" 
     + "and displays the number of times each" 
     + "vowel appears in the sentence"; 
    JOptionPane.showMessageDialog(null, display_message, "Lab 3 Description", JOptionPane.INFORMATION_MESSAGE); 



    // 4. visit each String posotion 

    do{ 

     // 3. input the character string 

     input_string = JOptionPane.showInputDialog("Enter the sentence to search"); 

     // 5. if position i of String is a vowel 
     // 6. increase the appropriate vowel counter 
     if (input_string.charAt(i) == 'a' || input_string.charAt(i) == 'A') 
     a_count++; 

     else if (input_string.charAt(i) == 'e' || input_string.charAt(i) == 'E') 
     e_count++; 

     else if (input_string.charAt(i) == 'i' || input_string.charAt(i) == 'I') 
     i_count++; 

     else if (input_string.charAt(i) == 'o' || input_string.charAt(i) == 'O') 
     o_count++; 

     else if (input_string.charAt(i) == 'u' || input_string.charAt(i) == 'U') 
     u_count++; 

     i++; 

     String display_message1 = input_string          // 7. display the String 
     + "\n\n" + "has " + input_string.length() + " characters.\n\n"   // 8. display the number of characters 
     + "There are \n" 
     + a_count + " a's,\n"             // 9. disaply the number of each vowel 
     + e_count + " e's,\n" 
     + i_count + " i's,\n" 
     + o_count + " o's, and\n" 
     + u_count + " u's.\n\n"; 

     JOptionPane.showMessageDialog(null, display_message1, "Lab 3 Description", JOptionPane.INFORMATION_MESSAGE); 

     yes = JOptionPane.showConfirmDialog(null, "Would you like to enter another string?\n\n", "Extra Credit", JOptionPane.YES_NO_OPTION); 

    } while (i < input_string.length()); 

      if (i == input_string.length()) 
      { 
       yes = JOptionPane.showConfirmDialog(null, "Would you like to enter another string?\n\n", "Extra Credit", JOptionPane.YES_NO_OPTION); 
       if (yes == 1) 
       { 
        input_string = JOptionPane.showInputDialog("Enter the sentence to search"); 
       } 
      } 






    } // end of main 
} // end of the class 

回答

1

在你的代碼有地方仍然可以執行循環的條件,如果我s 0.因爲是的從來沒有從我所看到的重新定義,你基本上有一個無限循環,當我退出字符串長度的界限時,你的代碼將會出錯。

而且不知道爲什麼你在循環的底部(這將執行x次,其中x = input_string.length())

考慮像有兩個JOptionPanes:

if(i == input_string.length()) { 
    //ask the user if they want to enter another string 
    if(the user selected yes){ 
     yes = 1; 
     //instantiate again with new input_string 
    } 
} 

另注:假設你想要顯示一條消息,詢問用戶是否想在迭代完第一個條目後輸入另一個字符串,那麼使用變量和條件似乎並不合適。

+0

謝謝 - 我做了修改。我到了那裏。我再也沒有任何錯誤,但循環過早停止。 –

+0

跟蹤執行和/或調試,並找出它爲什麼提前執行。特別要注意循環執行的次數與輸入字符串的長度有關。在循環的最後一次執行(在循環結束時),您應該打印出總元音計數並詢問用戶是否要輸入另一個字符串。如果用戶選擇是,請設置新的input_string,重置您的計數和您的i變量,並且循環將再次繼續。 – ryanlutgen