2014-11-18 1 views
2

나는 파일에서 숫자를 읽고 그 평균을 계산하는 프로그램을 작성 중이다. 그러나 런타임에 아래 오류가 나타납니다. 코드에 표시된대로 캐스팅을 사용하여 수정하려고했지만 도움이되지 않았습니다.평균을 계산하는 요소에 액세스하는 Java arraylist

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double 
    at ReadFile.main(ReadFile.java:34) 
    at __SHELL40.run(__SHELL40.java:6) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at bluej.runtime.ExecServer$3.run(ExecServer.java:774) 
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double 
    at ReadFile.main(ReadFile.java:34) 
    at __SHELL41.run(__SHELL41.java:6) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at bluej.runtime.ExecServer$3.run(ExecServer.java:774) 
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double 
    at ReadFile.main(ReadFile.java:33) 
    at __SHELL42.run(__SHELL42.java:6) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at bluej.runtime.ExecServer$3.run(ExecServer.java:774) 
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double 
    at ReadFile.main(ReadFile.java:33) 
    at __SHELL43.run(__SHELL43.java:6) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at bluej.runtime.ExecServer$3.run(ExecServer.java:774) 

코드 : numbersString 개체를 포함

import java.io.*; 
import java.util.Scanner; 
import java.util.ArrayList; 

public class ReadFile 
{ 
    public static void main(String[] args) 
    { 
     try 
     { 
      Scanner input = new Scanner("n.txt"); 
      File file = new File(input.nextLine()); 
      input = new Scanner(file); 
      ArrayList numbers = new ArrayList(); 

      int index=1; 

      while (input.hasNextLine()) { 
       String line = input.nextLine();; 
       numbers.add(line); 
       System.out.println(index + " : " + line); 
       index++; 
      } 
      input.close(); 

      double sum = 0.0; 
      double average; 
      for (int j = 0; j < numbers.size(); j++) 
      { 
       sum = sum + (double) numbers.get(j) ; //My guess is that this line is invoking the error 

      } 
      average = sum/numbers.size(); 
      System.out.println(average); 
     } 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 
} 

답변

4

목록, 그래서 double에 요소를 캐스팅 예외가 발생합니다. 이 컴파일러는 미리 타입 호환성을 감지 할 수 것

ArrayList<String> numbers = new ArrayList<String>(); 

: 제대로 제네릭 타입 ArrayList을 사용했다 경우이 런타임 예외를 피할 수 있습니다.

sum = sum + Double.parseDouble(numbers.get(j)); 

는 다른 방법이 Doublenumbers의 형식 매개 변수를 변경하고 목록에 요소를 추가 값으로 변환 :

문자열의 두 배 값을 읽으려면 Double#parseDouble(String) 사용할 수 있습니다

ArrayList<Double> numbers = new ArrayList<Double>(); 

... 
numbers.add(Double.parseDouble(line)); 
관련 문제