【发布时间】:2021-10-10 18:43:44
【问题描述】:
我正在尝试从 txt 文件中读取游戏的高分并将它们放入一个数组中,以便程序的另一部分可以检测到高分。早些时候我把所有东西都声明为 Int 并且它工作得很好,但我想要更精确,所以我决定让所有东西也能计算小数。但是,当我从 int 更改为 long 时,出现了此错误。我一直在寻找解决方案,但没有一个对我有用。有人可以看看吗?
供参考,txt文件包含
95,0
1,0
1,0
第 74 行出现错误“Exception in thread "AWT-EventQueue-0" java.util.InputMismatchException”,即
top3[i] = scanner.nextLong();
尽管 top3 被声明为 long。
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class WriteHighScore {
public static double[] top3 = new double[6];
public static boolean newTop3 = false;
public static void RewriteFile(double[] top3, boolean append) throws IOException {
//File file1 = new File(fileName);
//System.out.println("iniziando a riscrivere il file");
File file1 = new File("C:\\Users\\39340\\Desktop\\storageapp\\highscore", "highscores.txt");
FileWriter fw = new FileWriter(file1, append);
PrintWriter pw = new PrintWriter(fw);
//le tre linee sotto cancellano tutto
FileOutputStream writer = new FileOutputStream("C:\\Users\\39340\\Desktop\\storageapp\\highscore\\highscores.txt");
writer.write(("").getBytes());
writer.close();
//qui scrive il file nuovo
for (int i = 0; i < 3; i++) {
pw.println(top3[i]);
}
pw.close();
}
public static void AvgRewriteFile(double[] top3, boolean append) throws IOException {
//File file1 = new File(fileName);
//System.out.println("iniziando a riscrivere il file");
File file1 = new File("C:\\Users\\39340\\Desktop\\storageapp\\highscore", "averagehighscore.txt");
FileWriter fw = new FileWriter(file1, append);
PrintWriter pw = new PrintWriter(fw);
//le tre linee sotto cancellano tutto
FileOutputStream writer = new FileOutputStream("C:\\Users\\39340\\Desktop\\storageapp\\highscore\\averagehighscore.txt");
writer.write(("").getBytes());
writer.close();
//qui scrive il file nuovo
for (int i = 3; i < 6; i++) {
pw.println(top3[i]);
}
pw.close();
}
public static double[] main(double punti, double avgPunti) throws IOException {
//System.out.println("sono in writehighscore");
//in teoria sta cosa guarda che non si siano fine e ne crea uno se non c'è
File file = new File("C:\\Users\\39340\\Desktop\\storageapp\\highscore\\highscores.txt");
//if (!file.exists()) {saveToFile(punti, true); System.out.println("ho scritot i punti nel file");}
//questa roba legge il file e lo mette in un array
Scanner scanner = new Scanner(new File("C:\\Users\\39340\\Desktop\\storageapp\\highscore\\highscores.txt"));
for (int i = 0; i < 3; i++) {
top3[i] = scanner.nextLong(); //qui scrive
}
double exTop = 0;
double exSecondo = 0;
//System.out.println("sto iniziando a controllare i valori con punti = " + punti);
if (top3[0] < punti) {exTop =top3[0]; top3[0] = punti; newTop3 = true;} else {exTop = punti;}
if (top3[1] < exTop) {exSecondo = top3[1]; top3[1] = exTop; newTop3 = true;} else {exSecondo = exTop;}
if (top3[2] < exSecondo) {top3[2] = exSecondo; newTop3 = true;}
if (file.exists()) {RewriteFile(top3, true);}
File file2 = new File("C:\\Users\\39340\\Desktop\\storageapp\\highscore\\averagehighscore.txt");
Scanner scanner2 = new Scanner(new File("C:\\Users\\39340\\Desktop\\storageapp\\highscore\\averagehighscore.txt"));
for (int i = 3; i < 6; i++) {
top3[i] = scanner2.nextInt(); //qui scrive
//System.out.println("sto scrivendo nella top3. qui vale"+top3[i]);
//System.out.println("2top3 "+top3[i]);
}
double exAvgTop = 0;
double exAvgSecondo = 0;
//System.out.println("sto iniziando a controllare i valori con punti = " + punti);
if (top3[3] < avgPunti) {exAvgTop = top3[3]; top3[3] = avgPunti; newTop3 = true;} else {exAvgTop = avgPunti;}
if (top3[4] < exAvgTop) {exAvgSecondo = top3[4]; top3[4] = exAvgTop; newTop3 = true;} else {exAvgSecondo = exAvgTop;}
if (top3[5] < exAvgSecondo) {top3[5] = exAvgSecondo; newTop3 = true;}
if (file.exists()) {AvgRewriteFile(top3, true);}
if (newTop3 = true) {return top3;} else return null;
}
}
【问题讨论】:
-
A
long是具有更多位的整数类型(int为 64 对 32),因此它可以捕获更大的幅度 - 它不是浮点类型。对于后者,您将需要使用float或double并使用Scanner中的适当方法来读取该类型。我不知道Scanner是否会将逗号视为小数点 - 不过您的默认Locale可能会覆盖这些点。 -
好吧,我是个白痴,谢谢
-
top3[i] =scanner.nextDouble();给了我同样的错误
-
尝试将逗号 (,) 替换为句点 (.)。
-
我有点搞砸了,似乎该软件可以正常使用“,”但只要使用“。”发现它停止工作。问题是软件使用“。”当它重写文件时
标签: java java.util.scanner long-integer scanning inputmismatchexception