我正在尝试从名为Marathon_Data.txt的文本文件中读取数据,当我运行时,会出现此错误:
“线程中的异常”main“java.util.InputMismatchException” 我们还没有学会如何处理异常,所以到目前为止只使用了抛出IOException。这会导致这个问题吗? 感谢
import java.io.*;
import java.util.Scanner;
public class Marathon
{
final static int SIZE = 5;
public static void main(String[] args) throws IOException
{
int miles[][] = new int[SIZE][7];
String names[] = new String[SIZE];
//variables
int i=0, j=0;
int totalWeek = 0;
double average = 0.0;
//opening file
File file = new File("Marathon_Data.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
j=0;
names[i]=inputFile.next();
while(inputFile.hasNext())
{
miles[i][j]=inputFile.nextInt();
j++;
}
i++;
}
//Display total miles ran in each week by each runner and their average
System.out.println("Name Total Miles Average");
for(i=0;i<miles.length;i++)
{
System.out.print(names[i]);
for(j=0;j<miles[0].length;j++)
{
totalWeek +=miles[i][j];
}
average = (double)totalWeek / miles[0].length;
System.out.printf("%d\t\t%.2f\n", totalWeek, average);
totalWeek = 0;
}
}//end main method
}//end class