2015-01-22 4 views
-2

파일에서 숫자를 가져 오려고합니다 (할당시 data.txt가 지정됨). 해당 숫자의 최소/최대/평균을 취합니다. 내가 읽고있는 숫자로 ArrayList를 작성하는 데 어려움을 겪고있다.FileReader를 사용하여 ArrayList에 번호 추가

데이터 .txt 여기에 주어집니다 :

16 
4 
6 
5 
11 
8 
17 
7 
1 
10 
9 
15 
12 
13 
14 
2 
3 

import java.io.File; 
import java.util.Scanner; 
import java.io.IOException; 
import java.util.ArrayList; 
public class DataAnalyzer { 

    private String n; 

    public DataAnalyzer(String FileName){ 
     n = FileName; 

    } 
    public void read(ArrayList<Integer> list) throws IOException{ 
     File file = new File(n); 
     Scanner in = new Scanner(file); 

     list = new ArrayList<Integer>(); 

     while(in.hasNextLine()){ 
      String line = in.nextLine(); 

      Scanner scan = new Scanner(line); 
      while(scan.hasNextInt()){ 
       list.add(scan.nextInt()); 
      } 

      scan.close(); 
     } 
     in.close(); 
    } 
    public int getMin(ArrayList<Integer> list){ 
     int min = 100; 
     if (min == 100 && list.size() == 0){ 
      min = 0; 
     } 
     for(int i=0; i<list.size(); i++){ 
      if(list.get(i) < min) min = list.get(i); 
     } 

     return min; 
    } 
    public int getMax(ArrayList<Integer> list){ 
     int max = 0; 
     for(int i=0; i<list.size(); i++){ 
      if(list.get(i) > max) max = list.get(i); 
      } 
     return max; 
    } 
    public int getAvg(ArrayList<Integer> list){ 
     int total = 0; 
     int avg = 0; 
     if(list.size() == 0){ 
      avg = 0; 
     } 
     else{ 
     for(int i=0; i<list.size(); i++){ 
      total = list.get(i) + total; 
      } 
     avg = total/list.size(); 
     } 
     return avg; 
    } 
} 

import java.util.Scanner; 
import java.io.IOException; 
import java.util.ArrayList; 
public class DataAnalyzerTester { 
    public static void main(String[] args) throws IOException { 
     Scanner console = new Scanner(System.in); 

     System.out.println("Please enter a file to analyze: "); 
     String FileName = console.next(); 
     DataAnalyzer test = new DataAnalyzer(FileName); 

     ArrayList<Integer> list = new ArrayList<Integer>(); 
     test.read(list); 

     System.out.println("Min: " + test.getMin(list)); 
     System.out.println("Max: " + test.getMax(list)); 
     System.out.println("Avg: " + test.getAvg(list)); 

     console.close(); 
    } 

} 
+0

난 당신이 문제가 있다는 것을 상상할 것이다 (만약 그렇다면, 새로운 로컬 참조를 작성하지 않습니다)에 list 전달 수 read() 메서드에서 수정하는 메서드는 전달되는 개체와 다른 개체이며 반환되지 않습니다. – kirbyquerby

답변

0

당신은 호출자의 List 참조를 수정할 수 없습니다. 당신이 Listnew해야하는 경우가

public List<Integer> read() throws IOException{ 
    File file = new File(n); 
    Scanner in = new Scanner(file); 

    List<Integer> list = new ArrayList<Integer>(); 
    while(in.hasNextLine()){ 
     String line = in.nextLine(); 

     Scanner scan = new Scanner(line); 
     while(scan.hasNextInt()){ 
      list.add(scan.nextInt()); 
     } 

     scan.close(); 
    } 
    in.close(); 
    return list; 
} 

또한, 당신이 그 close() 호출을 수행 할 try-with-resources를 사용하는 것이 좋습니다 돌아갑니다. 그리고, 당신은 하나만 필요하다고 생각합니다 Scanner. 마지막으로

public List<Integer> read() throws IOException{ 
    File file = new File(n); 

    List<Integer> list = new ArrayList<Integer>(); 
    try (Scanner in = new Scanner(file)) { 
     while(in.hasNextInt()){ 
      list.add(in.nextInt()); 
     } 
    } 
    return list; 
} 

처럼, 당신은 목록 당신 때문에,

public void read(List<Integer> list) throws IOException{ 
    File file = new File(n); 
    try (Scanner in = new Scanner(file)) { 
     while(in.hasNextInt()){ 
      list.add(in.nextInt()); 
     } 
    } 
} 
+0

나는 지금 아주 간단한 바보 같지만 도움을 주셔서 대단히 감사합니다! 이것에 잠시 붙어있어 마침내 이해합니다. – UTDaveG