2012-11-21 3 views
0

죄송합니다. 코드가 좋지 않으면 프로그래밍에 익숙하지 않습니다. .txt의 텍스트를 날짜 - 이름 - 주소 등의 형식으로 전송해야합니다..txt에서 2D 배열로 읽기

나는 파일을 읽은 다음 문자열을 String.split ("-")로 분리합니다. 루프에 문제가 있습니다.

try{ 
     File file = new File("testwrite.txt"); 
     Scanner scan = new Scanner(file); 
     String[] test = scan.nextLine().split("-"); 
     while(r<100){ 
      while(c<6){ 
       data[r][c] = test[c]; 
       test = scan.nextLine().split("-"); 
       c++; 
      } 
      r++; 
      c = 0 ; 
     } 
     System.out.println(data[1][5]); 
    }catch(Exception e){ 
     System.out.println("Error: " + e.getMessage()); 
    } 
+0

System.out.println (data [1] [5]) - 테스트 용입니다. –

+0

'for'-loop처럼 보이는 것이 더 적합 할 것입니다. – arshajii

+0

우선 첫째 줄 8, test = scan.nextLine(). split ("-"); Test는 문자열 배열이므로 인덱스를 지정해야합니다. – Waleed

답변

2

2 차원 배열은 단지 "배열 배열"이므로 split 결과를 직접 사용하여 한 줄의 데이터를 저장할 수 있습니다.

  File file = new File("testwrite.txt"); 
      Scanner scanner = new Scanner(file); 
      final int maxLines = 100; 
      String[][] resultArray = new String[maxLines][]; 
      int linesCounter = 0; 
      while (scanner.hasNextLine() && linesCounter < maxLines) { 
       resultArray[linesCounter] = scanner.nextLine().split("-"); 
       linesCounter++; 
      } 
+0

감사합니다. 도움을 주셔서 감사합니다. –

0

너무 자주 scan.nextLine()을 호출하는 것처럼 보입니다. scan.nextLine()을 호출 할 때마다 스캐너는 현재 행을지나갑니다. 파일에 100 개의 행이 있다고 가정하고 각 행마다 한 번 호출되도록 test = scan.nextLine().split("-");을 while 루프의 끝 (그러나 여전히 루프 내부)으로 이동합니다.

편집 ...

솔루션 제안

: abcxyz

abcxyz 형태의 파일을 감안할 때 ,

(총 100 회)

사용이 코드 :

try{ 
    File file = new File("testwrite.txt"); 
    Scanner scan = new Scanner(file); 
    String[] test = scan.nextLine().split("-"); 
    while(r<100){ 
     while(c<6){ 
      data[r][c] = test[c]; 
      c++; 
     } 
     r++; 
     c = 0 ; 
     test = scan.nextLine().split("-"); 
    } 
    System.out.println(data[1][5]); 
}catch(Exception e){ 
    System.out.println("Error: " + e.getMessage()); 
} 

그런 다음 데이터 [행] [색인]을 사용하여 데이터에 액세스하십시오. 제외하고, 또는 다른 문자 - 나는 탭 (\의 t)에 분할하고 내 인스턴스에서, 다시

BufferedReader reader = new BufferedReader(new FileReader(path)); 
int lineNum = 0; //Use this to skip past a column header, remove if you don't have one 
String readLine; 
while ((readLine = reader.readLine()) != null) { //read until end of stream 
    if (lineNum == 0) { 
     lineNum++; //increment our line number so we start with our content at line 1. 
     continue; 
    } 
    String[] nextLine = readLine.split("\t"); 

    for (int x = 0; x < nextLine.length; x++) { 
     nextLine[x] = nextLine[x].replace("\'", ""); //an example of using the line to do processing. 

     ...additional file processing logic here... 
    } 
} 

하지만 당신은 그냥 간단하게에 분할 할 수 있습니다 : I 탭 분할

+0

"줄을 찾을 수 없음"오류가 발생합니다. –

0

는 다음을 사용하여 파일을 분리 개행 문자.

The Javadoc for readline()A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed..

필요에 따라 선을 분할 한 다음 필요에 따라 배열에 할당했습니다.