2014-12-07 2 views
0

1000 개의 항목이있는 파일에서 x, y 좌표를 읽으려고합니다.Convex Hull - 입력 파일에서 읽기

이것은 내가 지금까지 무엇을 가지고 :

int n=4; 
    Point2D []p = new Point2D[n]; 
    p[0] = new Point2D(4,5); 
    p[1] = new Point2D(5,3); 
    p[2] = new Point2D(1,4); 
    p[3] = new Point2D(6,1); 

나는이 같은 파일을 열 수 있습니다

Scanner numFile = new Scanner(new File("myValues.txt")); 
     ArrayList<Double> p = new ArrayList<Double>(); 
     while (numFile.hasNextLine()) { 
      String line = numFile.nextLine(); 
      Scanner sc = new Scanner(line); 
      sc.useDelimiter(" "); 
      while(sc.hasNextDouble()) { 
       p.add(sc.nextDouble()); 
      } 
      sc.close(); 
     } 
     numFile.close(); 

을하지만 두 값 각각의 시간 배열을 생성하는 방법을 모르겠어요. 자세한 정보가 필요하면 알려주십시오. 예를 들어

:

+0

을 명확하게. 자바처럼 보입니다. –

답변

0

당신이 정말로 루프의 각 반복에서 (당신이 .txt 파일의 좌표를 사용하여)의 Point2D 객체를 생성하기 만은, 다음의 Point2D 오브젝트의 배열 목록에 객체를 추가 :

ArrayList<Points2D> p = new ArrayList<>(); 

Scanner numFile = new Scanner(new File("myValues.txt")); 

String pointOnLine = numFile.readLine(); 

while (numFile != null) //if line exists 
{ 

    String[] pointToAdd = pointOnLine.split(" +"); //get x y coords from each line, using a white space delimiter 
    //create point2D object, then add it to the list 
    Point2D pointFromFile = new Point2D(Integer.parseInt(pointToAdd[0]), Integer.parseInt(pointToAdd[1])); 
    p.add(pointFromFile); 
    numFile = numFile.readLine(); //assign numFile to the next line to be read 

} 

까다로운 부분 (내가 있으리라 믿고있어에서 당신이 붙어있는 부분), 파일에서 각각의 x와 y 좌표를 추출한다.

내가 수행 한 작업은 .split() 메서드를 사용하여 모든 단일 줄을 공백으로 구분 된 전체 줄의 각 숫자 문자열 배열로 변환하는 것입니다. 각 행에는 두 개의 숫자 (x와 y) 만 포함되어야하므로 배열 크기는 2 (요소 0과 1)가됩니다.

거기에서 문자열 배열의 첫 번째 요소 (x 좌표)와 두 번째 요소 (y 좌표)를 얻은 다음 해당 문자열을 정수로 구문 분석합니다.

각 행에서 x와 y를 분리 했으므로이를 사용하여 Point2D 객체를 만든 다음 해당 객체를 배열 목록에 추가합니다.

희망이 당신이 언어로 질문을 다시 태그하는 경우가 더 많은 도움을 얻을 것이다 일

+0

대단히 고마워요! 이게 정말 도움이 됐어. – user01230

관련 문제