2017-11-15 10 views
-2

나는 문자열의 모음, 자음, 공백 및 구두점의 수를 세는 프로그램을 만들고 있습니다. 다른 것으로 옮기기 전에 모음 검사기가 작동하는지 확인하고 있습니다. 논리적으로 작동하는 것처럼 보이는 루프를 만들었습니다. 자바 시각화기를 통해 실행 했으므로 모든 것이 체크 아웃됩니다. 그러나 BlueJ IDE를 통해 실행하면 오류가 발생합니다. It says : java.lang.StringIndexOutOfBoundsException : 문자열 인덱스가 범위를 벗어났습니다. 나는 그 문제가 무엇인지 전혀 알지 못한다. 나는 모든 도움에 감사 할 것이다. 이 사람이 스스로를 테스트하고자하는 경우 java visualizer에 대한 링크, 그리고 나는 아래 코드는 게시 한 :Java에서 루프에 문제가 있습니다. 설명의 설명

//************************************************************** 
// Testing program for VCP program (temporary). 
// 
// @aaron_ford 
// @version_1.0_11.9.17 
//************************************************************** 
public class VCPTest 
{ 
    public static void main(String[] args) 
    { 
     System.out.println("Enter a string."); 
     String user_str = "a vowel is here"; 
     System.out.println("You entered: " + user_str); 

     char vowels[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; 

     // counter variables 
     int v_counter = 0; 
     int c_counter = 0; 
     int s_counter = 0; 
     int p_counter = 0; 
     int count = 0; 
     int str_compare = user_str.charAt(count); 

     for (; count < str_compare; count++) 
     { 
      str_compare = user_str.charAt(count); 
      for (int a:vowels) 
      { 
       if (a == str_compare) 
       { 
        v_counter++; 
       } 
      } 
     } 

     System.out.println("There are " + v_counter + " vowels."); 
     System.out.println("There are " + c_counter + " consonants."); 
     System.out.println("There are " + s_counter + " spaces."); 
     System.out.println("There are " + p_counter + " punctuation marks."); 
    }  
} 
+0

' 'str_compare = user_str.charAt (count)'일 때 shmosel

+0

for 루프 조건이'count

+0

다음을 시도해보십시오 : for (char v : bev) if (c == v) v_counter ++;' – shmosel

답변

0

문제는 당신 "에 대한"루프입니다. 카운트 값이 str_compare 값 (주어진 문자열의 문자 숫자 표현)보다 작은 지 확인합니다. 나는 이것이 당신이 기대했던 것이 아니라고 생각합니다. 약간의 수정을 제안합니다 - 지정된 문자열 (str_compare = user_str.charAt(count);)에서 문자를 확인하고 모든 문자열을 검색 한 후 count 값이이 문자열의 다음 문자를 검사하기 위해 증가되고 있습니다. 모든 루프에서 검증은 문자열의 끝이 현재 (count < user_str.length())

for (count = 0; count < user_str.length(); count++) 
     { 
      str_compare = user_str.charAt(count); 
      for (int a : vowels) 
      { 
       if (a == str_compare) 
       { 
        v_counter++; 
       } 
      } 
     } 

이제 프로그램의 출력이 보이는 도달 여부를 확인하기 위해이되어 더 나은 : 검사의 논리는 무엇

Enter a string. 
You entered: a vowel is here 
There are 6 vowels. 
There are 0 consonants. 
There are 0 spaces. 
+0

정말 고마워요! 이것은 내가 가진 문제를 해결했다. – Alphox

+0

그것은 그것이 당신을 도왔다, 당신을 위해 최선을 다하는 것이 좋다! –