2017-02-16 3 views
1

프로그램에서 나는 명시되지 않은 양의 입력에 포함 된 문자 쌍을 찾아야합니다. 대/소문자를 구분하지 않고 동일한 두 개의 영문자가 발견되면 26 행 26 열의 2 차원 배열 내의 요소에 하나를 추가합니다. 내가 "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz"내 프로그램 입력을 실행할 때 이런 일이 발생 무엇Java 2d 배열에 요소 오류 하나를 더함

import java.util.Scanner; 
public class Freq{ 
    private static final int ROWS = 26; 
    private static final int COLS = 26; 
    private static int[] [] alphabet = new int[ROWS][COLS]; 
    public static void main(String[] args) { 
     String line; 
     Scanner userInput = new Scanner(System.in); 
     while(userInput.hasNextLine()) { 
      line = userInput.nextLine(); 
      processLine(line); 

     } 
     printArray(); 

    } 
    public static void processLine(String line) { 
     line = line.toUpperCase(); 
     for(int i = 0; i < alphabet.length; i++) { 
      for(int j = 0; j < alphabet[i].length; j++) { 
       for (int a = 0; a < line.length() - 1; a++) { 
        char firstLetter = line.charAt(a); 
        char secondLetter = line.charAt(a + 1); 
        if (firstLetter == secondLetter) { 
         alphabet[firstLetter - 65][secondLetter - 65] += 1; 
        } 
       } 
      } 
     } 
    } 
    public static void printArray() { 
    for (int b = 0; b < alphabet.length; b++) { 
     for (int c = 0; c < alphabet[b].length; c++){ 
      System.out.print(alphabet[b][c] + " "); 
     } 
     System.out.println(); 
    } 
    } 
} 

: 여기 내 코드입니다

aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz 
676 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 676 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 676 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 676 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 676 0 0 0 0 0 0 0 0 0 0 0 0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0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 676 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 676 

나는 그들이 올바른 위치에 추가되고 있습니다 생각하지만, 왜 내 프로그램은 676을 색인에 추가하고 1을 더하지 않고? 어떤 도움이라도 대단히 감사합니다. 고맙습니다!

답변

2

귀하의 processLine() 방법은 타당하지 않습니다. 첫째, 당신은 단지 입력 문자열의 반복되어야하고, 전체 2 차원 배열, 즉하지 : 당신은 오직 할당을하기 때문에

public static void processLine(String line) { 
    line = line.toUpperCase(); 
    for (int a=0; a < line.length() - 1; a++) { 
     char firstLetter = line.charAt(a); 
     char secondLetter = line.charAt(a + 1); 
     if (firstLetter == secondLetter) { 
      alphabet[firstLetter - 65][secondLetter - 65] += 1; 
     } 
    } 
} 

둘째, 당신의 2 차원 배열은 오직, 대각선에 항목이 있습니다 두 문자는 두 가지 차원에서 동일합니다. 그래서 당신은 단지 1 차원 배열을 사용할 수 있습니다이 줄을 교체 단지를 지정하는 장소에서 당신이 일을하려는 대신하는 경우

private static int[] alphabet = new int[ROWS]; 

public static void processLine(String line) { 
    line = line.toUpperCase(); 
    for (int a=0; a < line.length() - 1; a++) { 
     char firstLetter = line.charAt(a); 
     char secondLetter = line.charAt(a + 1); 
     if (firstLetter == secondLetter) { 
      alphabet[firstLetter - 65] += 1; 
     } 
    } 
} 
+0

와우 나는 너무 바보 같은 느낌. 도와 줘서 고마워. 또한, "aaaa"가 있다면, 배열에있는 첫 번째 요소를 한 번만 추가하기를 원합니다. – Coder117

+0

'aaaabbbbaaaa'의 경우 어떻게해야합니까? 'aa' 또는 단지 하나에 대해 두 개의 항목이 있습니까? –

+0

그냥 한 줄당 문자 쌍을 한 번 찾으려고합니다. – Coder117

1

무엇 당신이하고있는 것은 당신이 1 매트릭스에 그 위치에서 값을 추가하고 있는지를

,536이

alphabet[firstLetter - 65][secondLetter - 65] = 1; 

또는 변경하려면 processLine 방법으로

alphabet[firstLetter - 65][secondLetter - 65] += 1; 

public static void processLine(String line) { 
     line = line.toUpperCase(); 
      for (int a = 0; a < line.length() - 1; a++) { 
        char firstLetter = line.charAt(a); 
        char secondLetter = line.charAt(a + 1); 
        if (firstLetter == secondLetter) { 
         alphabet[firstLetter - 65][secondLetter - 65] += 1; 
      } 
     } 
    } 

희망 답변이 질문에 대한 답변입니다.