2012-12-20 3 views
0

내가 달성하기 위해 필요한 것은 다음과 같은 두 배의 두 개의 열이 포함 된 텍스트 파일에서 데이터를 가져 오는 것입니다 :배열로 텍스트 파일에서 특정 열을 얻기

이 약 50,000 선까지 있습니다
201.0176 1.06E+03 
201.7557 1.11E+01 
202.0201 2.02E+02 
202.2064 9.76E+00 
202.8342 1.11E+01 
203.0161 2.02E+02 
203.1638 9.61E+00 
203.3843 1.13E+01 

데이터. 각 열을 별도의 배열로 가져 오길 원하지만 별도의 열을 식별하는 방법을 찾지 못합니다. 나는 출력을 얻을

public class CodeTests { 

    public static ArrayList assignArray(String filePath) throws FileNotFoundException { 
     Scanner s = new Scanner(new File(filePath)); 
      ArrayList<Double> list = new ArrayList<Double>(); 
      while (s.hasNext()){ 
       list.add(s.nextDouble()); 
      } 
      s.close(); 
      return list; 
    } 

    public static void main(String[] args) throws IOException { 
     /* 
     * 
     */ 
     ArrayList arrayMZ; 
     arrayMZ = assignArray("F:/Stuff/Work/Work from GSK/From Leeds/ja_CIDFragmenter/testFile.txt"); 

     for(int i = 0; i < arrayMZ.size(); i++) 
      System.out.println(arrayMZ.get(i)); 
     System.out.println("End"); 
    } 
} 

이 실행에서 : 나는 다음과 같은 시도

run: 
201.0176 
1060.0 
201.7557 
11.1 
202.0201 
202.0 
202.2064 
9.76 
202.8342 
11.1 
203.0161 
202.0 
203.1638 
9.61 
203.3843 
11.3 
End 
BUILD SUCCESSFUL (total time: 1 second) 
사람이 별도의 두 배열에이 열 또는의 열에서 하나의 2 차원 배열로 하나 도와 줄 수

array [0]에는 첫 번째 데이터 열이 있고 array [1]에는 두 번째 데이터 열이 있습니다. 즉 :

[0]   [1] 
201.0176 1.06E+03 
201.7557 1.11E+01 
202.0201 2.02E+02 
202.2064 9.76E+00 
202.8342 1.11E+01 
203.0161 2.02E+02 
203.1638 9.61E+00 
203.3843 1.13E+01 

나는 내가 할 수있는 아무것도하지만,이 경우 다른 알려 주시기 바랍니다 등이 명확하게하기 위해 노력했습니다.

감사와 같은

답변

1

당신은 이런 식으로 뭔가를 할 수 :

public static ArrayList[] assignArray(String filePath) throws FileNotFoundException { 
    Scanner s = new Scanner(new File(filePath)); 
     ArrayList[] list = new ArrayList[2]; 
     ArrayList<Double> col1 = new ArrayList<Double>(); 
     ArrayList<Double> col2 = new ArrayList<Double>(); 
     while (s.hasNext()){ 
      String[] data = s.nextLine().split("\\s+"); 
      col1.add(Double.parseDouble(data[0])); 
      col2.add(Double.parseDouble(data[1])); 
     } 
     s.close(); 
     list[0] = col1; 
     list[1] = col2; 
     return list; 
} 

하고 데이터를 두 개의 ArrayList를 배열을 얻을 수 있습니다.

+0

감사합니다. 전에 ArrayList를 사용 해 본적이 없습니다. 정말 유용합니다. – Primigenia

0

뭔가 :

for(int i = 0; i < arrayMZ.size()/2; i+=2) 
    System.out.printf("%.4f\t%E\n", arrayMZ.get(i), arrayMZ.get(i + 1)); 
+0

그런 식으로 배열을 인쇄 할 것이지만 나중에 ** 더 많은 코드를 쓸 수 있도록 더 논리적 인 방법으로 텍스트 파일에서 ** 데이터 **를 저장하고 싶습니다. 배열. – Primigenia

+0

아, 죄송합니다. 질문이 잘못되었습니다. – maverik

관련 문제