2015-01-23 3 views
0
내가 파일에 읽고 차원의 배열에 각 라인의 내용을 기록 할

[세] [부정하지만, 매우 긴] 지금까지 내가 코드를 다음 한동적 2D 자바 배열을 채우는

, 패턴 매처 (pattern matcher)를 사용하여 내가 찾고있는 입력 파일의 구성 요소를 찾아 낼 수는 있지만, 이것은 입력의 첫 번째 줄에 고착되어 반복적으로 입력 파일의 진행을 만드는 방법을 추가합니다 매번 배열에 새 행을 씁니다.

지금까지 내 코드는 다음과 같다 :

public static void main(String[] args) throws IOException 
{ 

    BufferedReader br_0 = new BufferedReader(new FileReader("file.txt")); 
    String line_0; 

    //while the file is still reading 
    while ((line_0 = br_0.readLine()) != null) 
    {   

     int i = 0; 
     Pattern p = Pattern.compile("'(.*?)'(?![a-zA-Z])"); 
     //count from zero 
     String[][] arr = new String[262978][3]; 

     for (int count = 0; count < 262978; count++) 
     { 

      Matcher m = p.matcher(line_0); 

      int j = 0; 
      while (m.find()) 
      { 
       arr[i][j++] = m.group(1); 
      } 
      i++; 

     } 
    } 
    br_0.close(); 
} 

입력 파일은 다음과 같다 :

'end with'('the playing of the british national anthem', 'hong kong'). 
'follow at'('the stroke of midnight', 'this'). 
'take part in'('the ceremony', 'both countries'). 
'start at about'('# pm', 'the ceremony'). 
'end about'('# am', 'the ceremony'). 
'lower'('the british hong kong flag', '# royal hong kong police officers'). 
'raise'('the sar flag', 'another #'). 
'leave for'('the royal yacht britannia', 'the #'). 
'hold by'('the chinese and british governments', 'the handover of hong kong'). 
'rise over'('this land', 'the regional flag of the hong kong special administrative region of the people \'s republic of china'). 
'cast eye on'('hong kong', 'the world'). 
'hold on'('schedule', 'the # governments'). 
'be festival for'('the chinese nation', 'this'). 
'go in'('the annals of history', 'july # , #'). 
... 

이상적으로 배열 인덱스는 다음과 같을 것이다 :

[0] 0] end with

[0] [1] the playing of the british national anthem,210

[0] [2] hong kong

[1] [0] follow at

[1] [1] the stroke of midnight

[1] [2] this

[2- [0] take part in

[3] [1] the ceremony

[2] [2] both countries

매우 긴 파일은 물론 짧은 파일도 수용 할 수 있어야합니다.

이 같은이 시점 모양의 출력은 :

[45993][2] the president of the people \'s republic of china he mr jiang zemin 
[45994][0] speak at 
[45994][1] the ceremony 
[45994][2] the president of the people \'s republic of china he mr jiang zemin 
[45995][0] speak at 
[45995][1] the ceremony 
[45995][2] the president of the people \'s republic of china he mr jiang zemin 
[45996][0] speak at 
[45996][1] the ceremony 
[45996][2] the president of the people \'s republic of china he mr jiang zemin 
[45997][0] speak at 
[45997][1] the ceremony 
[45997][2] the president of the people \'s republic of china he mr jiang zemin 
[45998][0] speak at 
[45998][1] the ceremony 
[45998][2] the president of the people \'s republic of china he mr jiang zemin 
[45999][0] speak at 
+0

실제 출력을 추가 (또는 :-)을) – xmoex

답변

4

이 첫 번째 줄을 262,978 시간을 처리합니다.

int count = 0; 
String[][] arr = new String[262978][3]; 
Pattern p = Pattern.compile("'(.*?)'(?![a-zA-Z])"); 
//while the file is still reading 
while ((line_0 = br_0.readLine()) != null) {   
    Matcher m = p.matcher(line_0); 
    int j = 0; 
    while (m.find()) { 
     arr[count][j++] = m.group(1); 
    } 
    count++; 
} 

br_0.close(); 

그러나, 매직 넘버 262978는 사용하지 않아야하고, 어느 배열 :

for (int count = 0; count < 262978; count++) 

더 나은입니다. 분명히 또한 최대의 가정. 한 줄에 3 개의 문자열이 올바르지 않습니다.

, 인쇄하려면

List<List<String>> arr = new ArrayList<>(); 
Pattern p = Pattern.compile("'(.*?)'(?![a-zA-Z])"); 
//while the file is still reading 
while ((line_0 = br_0.readLine()) != null) { 
    List<String> three = new ArrayList<>();   
    Matcher m = p.matcher(line_0); 
    int j = 0; 
    while (m.find()) { 
     three.add(m.group(1)); 
    } 
    arr.add(three); 
} 

br_0.close(); 

에 의해

for(List<String> three: arr){ 
    for(String s: three){ 
     System.out.print(s + " "); 
    } 
    System.out.println(); 
} 
+1

을하고 더 나은이와 함께 문자열 배열을 대체하는 것입니다하시기 바랍니다 커스텀 클래스 – Steffen

+0

리스트를 반복하면 확장 된 대답을 볼 수 있습니다. – laune

+0

이 오류가 다시 발생합니다 : 3 행의 java.lang.ArrayIndexOutOfBoundsException [j ++] = m.group (1); –

0

을이 교체 여기에있는 파일에서 데이터를 읽고 : while ((line_0 = br_0.readLine()) != null)하지만, 당신이 여기에 같은 라인 262978 반복을하고 있습니다 : for (int count = 0; count < 262978; count++).

당신이 무엇을 할 수 있는지, 그래서 같이 무언가로 대체하는 것입니다 :

int i = 0; 
Pattern p = Pattern.compile("'(.*?)'(?![a-zA-Z])"); 
//count from zero 
String[][] arr = new String[262978][3]; 
while (((line_0 = br_0.readLine()) != null) && (i < 262978)) 
{ 
    Matcher m = p.matcher(line_0); 

    int j = 0; 
    while (m.find()) 
    { 
     arr[i][j++] = m.group(1); 
    } 
    i++;  
} 
관련 문제