2013-03-07 3 views
0

텍스트 파일의 데이터를 읽고 2 차원 배열에 넣는 데 문제가 있습니다. 데이터 세트의 샘플은 다음과 같습니다Java의 텍스트 파일에서 데이터 읽기

1,2,3,4,5,6

1.2,2.3,4.5,5.67,7.43,8

이 코드의 문제가

그것은 단지 첫 번째 라인을 읽고 다음 라인을 읽지 않는다는 것입니다. 모든 제안을 부탁드립니다.

package test1; 
import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

public class Test1{ 

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

double[][] X = new double[2][6]; 
BufferedReader input = new BufferedReader(new FileReader(file)); 

String [] temp; 
String line = input.readLine(); 
String delims = ","; 
temp = line.split(delims); 
int rowCounter = 0; 
while ((line = input.readLine())!= null) { 
for(int i = 0; i<6; i++){ 
X[rowCounter][i] = Double.parseDouble(temp[i]); 
} 

rowCounter++; 
} 

}catch (Exception e){//Catch exception if any 
    System.err.println("Error: " + e.getMessage()); 
}finally{ 
} 
} 
} 
+1

어떻게 첫 번째 줄만 읽고 있다고 진단 했습니까? 필자가 읽는 모든 라인에 대해 임시 배열을 다시 생성하지 않기 때문에 매 반복마다 첫 번째 행을 처리하는 것처럼 보입니다. –

+0

정확 하 게, 당신은 절대적으로 옳습니다. 문제는 모든 라인에 대해 임시 배열을 어떻게 다시 만들어야하는지에 대한 지식입니다. – MTT

+0

코드 블록에 일관되고 논리적 인 들여 쓰기를 사용하십시오. 코드의 들여 쓰기는 사람들이 프로그램 흐름을 이해하도록 돕기위한 것입니다. –

답변

1

시도 :

int rowCounter = 0; 
while ((line = input.readLine())!= null) { 
String [] temp; 
String line = input.readLine(); 
String delims = ","; 
temp = line.split(delims); 
for(int i = 0; i<6; i++){ 
X[rowCounter][i] = Double.parseDouble(temp[i]); 
} 
... 
2

당신이 배열 유틸리티를 시도? 이런 식으로 뭔가 :

while ((line = input.readLine())!= null) { 
    List<String> someList = Arrays.asList(line.split(",")); 
    //do your conversion to double here 
    rowCounter++; 
} 

나는 빈 줄이

1

당신의 temp 배열이 당신의 while 루프 앞에있는 할당되고있는 유일한 장소 떨어져 루프 당신을 던지는 것 같아요. 루프 내에 temp 배열을 할당해야하며 루프까지 BufferedReader에서 읽지 마십시오.

String[] temp; 
String line; 
String delims = ","; 
int rowCounter = 0; 
while ((line = input.readLine())!= null) { 
    temp = line.split(delims); // Moved inside the loop. 
    for(int i = 0; i<6; i++){ 
    X[rowCounter][i] = Double.parseDouble(temp[i]); 
} 
0

readLine은 줄 끝 부분에 줄 바꿈 문자가 있어야합니다. 마지막 행을 읽거나 대신 read를 사용하려면 빈 행을 두어야합니다.

0

코드를 실행할 수 없지만 첫 번째 텍스트 줄만 분할하는 것이 문제입니다.

package Test1; 

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

public class Test1 { 

    public static void main(String args[]) { 
     try { 
      double[][] X = new double[2][]; 
      BufferedReader input = new BufferedReader(new FileReader(file)); 

      String line = null; 
      String delims = ","; 

      int rowCounter = 0; 
      while ((line = input.readLine()) != null) { 
       String[] temp = line.split(delims); 
       for (int i = 0; i < temp.length; i++) { 
        X[rowCounter][i] = Double.parseDouble(temp[i]); 
       } 
       rowCounter++; 
      } 

     } catch (Exception e) {// Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
      e.printStackTrace(); 
     } finally { 
     } 
    } 
} 

코드를보다 읽기 쉽게 포맷했습니다.

나는 2 차원 배열의 두 번째 요소의 크기를 줄 때 얼마나 많은 숫자가 한 줄에 있는지 알 때까지 연기했다.

관련 문제