2012-03-19 5 views
0

나는 두 배의 목록이있는 텍스트 파일을 가지고 있습니다. 선의 첫 번째 값은 x 값이고 두 번째 값은 y 값입니다.arrayList에로드가 두 배로 늘어납니다.

103.0 274.0 
    133.0 383.0 
    342.0 250.0 
    204.0 126.0 
    177.0 357.0 
    ... 

어떻게이 값을 읽고 배열 목록에로드 할 수 있습니까?

ArrayList<Point> store = new ArrayList<Point>(); 


        File file = fc.getSelectedFile(); 

       StringBuilder all = new StringBuilder(); 

       BufferedReader reader = new BufferedReader(new FileReader(file)); 
       String input = null; 
       while ((input = reader.readLine()) != null) 
       { 
       String a = all.append(input+"\n").toString(); 

       String[] hold = a.split(" "); 

       double x = Double.parseDouble(hold[0]); 
       int aa = (int)(x); 
       double y = Double.parseDouble(hold[1]); 
       int bb = (int)(y); 

       store.add(new Point(aa, bb)); 
       } 
+1

이다 http://stackoverflow.com/questions/1855753/reading- double-values-from-file –

+0

코드를 포맷 할 때 좀 더주의를 기울여주십시오. (이 포럼은 탭을 부드럽게 처리 할 수 ​​없습니다. 특히 공백으로 섞을 때 ---) 볼 수 있듯이 – kleopatra

답변

1
1: Get an input stream of the file. 
2: read line by line using readLine() method 
3: split the string by SPACE using split() method of String class 
4: the String array you got has two elements now. 
5: Double.parseDouble(array[0]) and Double.parseDouble(array[1]) are value of x and y 
6: store these values into the corresponding values of Point object 
7: add that point object to ArrayList. 
8: Done 
+0

글쎄요. 약간. 제대로 작동하지 않습니다. 위 참조. – chief

+0

@chief 무엇이 보여주고있는 문제입니까? –

+0

"AWT-EventQueue-0"스레드의 예외 java.lang.NumberFormatException : 입력 문자열의 경우 : "178.0 306.0"파일의 첫 번째 줄은 349.0 178.0이고 파일의 두 번째 줄은 306.0 159.0 – chief

0

당신은 자바 리소스 번들을 사용할 수 있습니다.

1

자바 스캐너와 nextDouble()를

스캐너 파일 nextDouble 파라미터()를 판독하여 사용한다 읽기 파일의 데이터 형식

관련 문제