【问题标题】:InputMismatchException in a scanner to get data from txt file扫描仪中的 InputMismatchException 从 txt 文件中获取数据
【发布时间】: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),因此它可以捕获更大的幅度 - 它不是浮点类型。对于后者,您将需要使用floatdouble 并使用Scanner 中的适当方法来读取该类型。我不知道 Scanner 是否会将逗号视为小数点 - 不过您的默认 Locale 可能会覆盖这些点。
  • 好吧,我是个白痴,谢谢
  • top3[i] =scanner.nextDouble();给了我同样的错误
  • 尝试将逗号 (,) 替换为句点 (.)。
  • 我有点搞砸了,似乎该软件可以正常使用“,”但只要使用“。”发现它停止工作。问题是软件使用“。”当它重写文件时

标签: java java.util.scanner long-integer scanning inputmismatchexception


【解决方案1】:

您收到InputMismatchException 异常,因为您尝试将字符串95,0 读取为long 值。但是长数字中没有, 字符。正如您之前使用的pw.println(top32[i]);,其中top32[] 是一个double 数组,它在文件中写为double 值,带有, 字符。

根据您要执行的操作,您可以通过在将值写入文件之前将值转换为 long/int 值的方式将其写入文件中,或者您可以使用 @ 987654333@ 将值读取为 double 值,然后在需要时将其转换为 long/int

【讨论】:

  • top3[i] =scanner.nextDouble();给了我同样的错误
  • 您可能需要检查和/或设置Scanner 对象的区域设置。 PrintWriter 使用的语言环境使用, 作为小数分隔符,但Scanner 对象可能使用使用. 作为小数分隔符的语言环境。有关此问题,请参阅 docs.oracle.com/javase/8/docs/api/java/util/…
猜你喜欢
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多