该程序编译好但后来, 运行代码后,我收到一组错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at ParkRunnerClass.main(ParkRunnerClass.java:48)
根据我的理解,这是因为进入的数据与扫描仪正在寻找的数据不匹配,为了解决这个问题我尝试使用try(在mycode中)并捕获但是没有用。
这是我以前的工作的积累,首先尝试而不会给我带来任何问题,当我检查它停止的新文件时:
参赛者数量为:1
Runner条形码是:A0000005
跑步者的名字是:
凭借用户输入,代码可以正常工作 所以问题似乎与名称的输入有关。 我不确定应该如何解决这个问题。
这是存储在file.txt中的短输入, 为了清楚起见,较长的版本有10个“跑步者”将评论值,但这些评论不在文件中
1 //跑步者数量
Mary Bloggs //亚军名称
1996年//亚军诞生年
3我不知道这个值是什么,所以我只是接受并按原样把它拿出来
2我不知道这个值是什么,所以我只是接受并按原样把它拿出来
女//跑步者性爱
5 //跑步者做了这个,然后设定循环运行的次数
Marley Park //公园名称
23.5
马利公园22.9
马利公园23.6
马利公园23.0
马利公园22.99
public class ParkRunnerClass
{
public static void main(String args[]) throws IOException
{
//Scanner input = new Scanner(System.in);
Scanner fileInput = new Scanner(new File("file.txt"));
PrintStream printStreamOut = new PrintStream(new File("outputfile.txt"));
String Sid = "A000000"; // barcode template
//-----Park Runner ----//
ParkRunner parkrunner1 = new ParkRunner(); // runner object
RecordNewRun runnermm = new RecordNewRun(); //min max object
RecordNewRun[] runners = new RecordNewRun[5]; // runner array
float[] times = new float[runners.length]; // size of the array for calc avg min/mix
float avg = 0;
float sum = 0;
int inputInt;
int inputIntrun;
int LapCount;
inputIntrun = fileInput.nextInt();
printStreamOut.println("Number of runners is:" + inputIntrun);
do {
parkrunner1.setRunnerId(Sid);
printStreamOut.println("Runner barcode is: " + parkrunner1.getRunnerId()); //runner barcode checked
parkrunner1.setName(fileInput.nextLine());
printStreamOut.println("The name for runner is: " + parkrunner1.getName());
parkrunner1.setAge(fileInput.nextInt());
printStreamOut.println("runner age is:" + parkrunner1.getAge());
inputInt = fileInput.nextInt();
printStreamOut.println("i dont know what this number is for:" + inputInt); // used for holding int
inputInt = fileInput.nextInt();
printStreamOut.println("i dont know what this number is for:" + inputInt); // used for holding int
parkrunner1.setGender(fileInput.next().charAt(0));
printStreamOut.println("runner gender M for male F for female:" + parkrunner1.getGender());
parkrunner1.setLaps(fileInput.nextInt());
LapCount = parkrunner1.getLaps();
printStreamOut.println("Laps ran by runner is:" + parkrunner1.getLaps());
for(int i = 0; i < LapCount; i++)
{
parkrunner1.setParkName(fileInput.nextLine());
printStreamOut.println("park name is: " + parkrunner1.getParkName());
//printStreamOut.println("\nPlease enter the time taken of a park run : ");
times[i] = fileInput.nextFloat();
sum = sum + times[i];
runnermm.setSum(sum);
runnermm.setAvg(sum);
//input.nextLine();
}
runnermm.getMinMax(times); // puts int array values into ohject runnermm (minmax) of RecordNewRun
printStreamOut.println("\nThe sum of park runners time spend running is: " + runnermm.getSum());
printStreamOut.println("\nThe average time of park runners time spent per lap is: " + runnermm.getAvg());
}while(fileInput.hasNext());
fileInput.close();
}
}