2014-03-01 2 views
0

Ok - 솔루션에 상당히 가깝지만 올바른 방향으로 전환이 필요하다는 것을 알고 있습니다. 나는 사용자가 YES를 누르고 프로그램이 질문을 다시하기 시작할 필요가있다.while 루프 JOptionPane 문제

실행 후, 내가 스레드에서 "주요"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

당신은 루프가 계속 실행될 수있는 조건을 가지고 있다면 0을 의미합니다. 은 내가 볼 수있는 것으로부터 결코 재정의되지 않습니다. 기본적으로 무한 루프가 발생하고 문자열 길이의 경계를 벗어나면 코드가 오류가 발생합니다. 또한

당신이

같은 고려 (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

실행 및/또는 디버그를 추적하고 왜 일찍 실행되는지 찾으십시오. 특히 입력 문자열의 길이와 관련하여 루프가 실행되는 횟수에주의하십시오. 루프가 끝날 때 루프의 마지막 실행에서 총 모음 수를 출력하고 사용자가 다른 문자열을 입력 할 것인지 묻습니다. 사용자가 yes를 선택하면 새로운 input_string을 설정하고 count와 i 변수를 재설정하면 루프가 다시 계속됩니다. – ryanlutgen