提问人:Guido 提问时间:8/6/2021 最后编辑:Guido 更新时间:8/6/2021 访问量:64
InputMismatchException 在扫描程序中从 txt 文件中获取数据
InputMismatchException in a scanner to get data from txt file
问:
我正在尝试从 txt 文件中读取游戏的高分并将它们放入数组中,以便程序的另一部分可以检测到高分。早些时候,我将所有内容都声明为 Int,它工作正常,但我想要更高的精度,所以我决定让所有东西都尽可能长地计算小数。但是,当我从int更改为long时,出现了此错误。我一直在寻找解决方案,但没有一个适合我。谁能看一下?
作为参考,txt 文件包含
95,0
1,0
1,0
错误“线程”AWT-EventQueue-0“java.util.InputMismatchException中的异常出现在第 74 行,即
top3[i] = scanner.nextLong();
尽管 Top3 被声明为长。
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;
}
}
答:
-1赞
Progman
8/6/2021
#1
您收到异常是因为您尝试将字符串作为值读取。但是很长的数字中没有字符。正如您之前使用的,其中是一个数组,它被写入文件中的值,并带有字符。InputMismatchException
95,0
long
,
pw.println(top32[i]);
top32[]
double
double
,
根据您要执行的操作,您可以通过在将值写入文件之前将值作为 / 值写入文件来更改它,或者您可以使用将值作为值读取,然后根据需要将其转换为 /。long
int
nextDouble()
double
long
int
评论
0赞
Guido
8/6/2021
top3[i] = scanner.nextDouble();给了我同样的错误
0赞
Progman
8/6/2021
您可能需要检查和/或设置对象的区域设置。用作小数分隔符的区域设置,但对象可能使用用作小数分隔符的区域设置。有关此问题,请参阅 docs.oracle.com/javase/8/docs/api/java/util/...。Scanner
PrintWriter
,
Scanner
.
评论
long
int
float
double
Scanner
Scanner
Locale