2016-11-27 1 views
0

여러 줄의 텍스트를 가져와 각 줄마다 각 문자의 개수를 계산해야합니다. 마지막 줄은 한 마디로 끝나야합니다. 나는 길이가 26 인 int 배열을 만들도록 요청 받았는데, arrayName[0] = number of a's, arrayName[1] = number of b's 등과 같은 대소 문자는 무시해야한다. 각 문자의 발생을 확인하고 정확한 발생 횟수로 색인 된 변수를 정의하는 데 문제가 있습니다. 지금까지 내 코드 :java - 문자열의 문자 수를 int 배열로 정렬 26

import java.util.Scanner; 

public class LetterCounter 
{ 
public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 

    String[] textBlock = new String[10]; 

    System.out.println("Enter text."); 
    int index; 
    //Allows me to type in something for all "lines" of text until I end one with a period. 
    for(index = 0; index < textBlock.length; index++) 
    { 
     textBlock[index] = input.nextLine(); 

     if(textBlock[index].contains(".")) 
     { 
      System.out.println("Period found"); 
      break; 
     } 
    } 
    //Outputs what the user printed except for the lines not typed in (they return null). 
    System.out.println("Text input by user:"); 
    for(index = 0; index < textBlock.length; index++) 
    { 
     if(textBlock[index] != null) 
     { 
      System.out.println(textBlock[index]); 
     } 
    } 
    //int array 
    int[] occurrences = new int[26]; 

    //used to turn letter into number 
    char letter = 'a' - 49; 
    char letter2 = 'b' - 49; 
    char letter3 = 'c' - 49; 

    //checks that numbers are correct (return "0" for a, "1" for b, and "2" for c) 
    System.out.println("Number value of a: " + letter); 
    System.out.println("Number value of b: " + letter2); 
    System.out.println("Number value of c: " + letter3); 

}// End of main 
}//End of program 
+0

, 나는 당신의 선언을 할 생각 루프 외부의 배열. 당신은 아마 당신이 소문자를 읽는 각 편지를 강요하고 싶다. 그런 다음 "a"에서 'z'로 문자를 검사 한 후, 읽기 문자의 색인 인 ++ 배열을 'a'로합니다. –

+0

비슷한 질문이 있습니다. http://stackoverflow.com/questions/33770180/how-do -i-store-these-printed-letters-an-array/33770240 # 33770240 – markspace

답변

1

모든 줄의 문자 발생 횟수를 센다. 그냥 인쇄해라. 이것을 시도 ...

//final int SIZE = 1000; 
//String[] textBlock = new String[SIZE]; 

Scanner in = new Scanner(System.in); 
String line; 

//frequency array for storing which Character is occurs how many times 
int[] frequencyArray = new int[26]; 
Arrays.fill(frequencyArray, 0); 

System.out.println("Enter text : "); 

// take input un-till find the (.) in a line 
// and also count the frequency of Character of current line 
while (true) { 
    line = in.nextLine(); 
    for (int i = 0; i < line.length(); i++) { 
     char ch = Character.toLowerCase(line.charAt(i)); 
     if (Character.isLetter(ch)) { 
      frequencyArray[ch - 'a']++; 
     } 
    } 
    if (line.contains(".")) { 
     break; 
    } 
} 

for (int i = 0; i < 26; i++) { 
    System.out.println("Total Number of " + (char)(i + 'a') + " : " + frequencyArray[i]); 
} 
0

루프 외부 배열을 선언하고 캐릭터가 당신의 발행 수를 찾을 수

int intialCount = occurrences(charAt(textBlock[index])-49); 
occurrences(charAt(textBlock[index])-49) = intialCount++; 

루프 내부에이 선언

을 MATHES 때 배열의 수를 증가 occurences [0]에서 'a'는 카운트를 반환합니다.

0

를 디버그 메시지를 출력하지 않는 경우, 당신은 단지 한 라인으로 작업을 수행 할 수 있습니다

우선 들어
System.out.println("Enter lines of text (period to end):"); 
new Scanner(System.in).useDelimiter("\\.").next().chars() 
    .filter(i -> i >= 'a' && i <= 'c').boxed() 
    .collect(Collectors.groupingBy(i -> i, Collectors.counting()) 
    .forEach((c, n) -> System.out.format("Number value of %s: %d\n", (char)c, n)); 
관련 문제