我正在编写一个程序,从文件中读取数字并计算它们的平均值。但是,我在运行时收到下面显示的错误。我尝试使用转换来修复它,如代码中所示加倍,但它没有帮助。
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)
代码:
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();
}
}
}
答案 0 :(得分:4)
列表numbers
包含String
个对象,因此将元素强制转换为double
会引发异常。如果您已正确使用泛型类型ArrayList
,则可以避免此运行时异常:
ArrayList<String> numbers = new ArrayList<String>();
这将允许编译器提前检测到类型不兼容。
要阅读字符串的double值,您可以使用Double#parseDouble(String)
:
sum = sum + Double.parseDouble(numbers.get(j));
或者,您可以将numbers
的类型参数更改为Double
,并在将该元素添加到列表时转换该值:
ArrayList<Double> numbers = new ArrayList<Double>();
...
numbers.add(Double.parseDouble(line));