0

I'm trying to read the highscores of a game from a txt file and put them into an array so that another section of the program can detect a high score. Earlier I had everything declared as Int and it worked fine but I wanted more precision so I decided to have everything as long to calculate decimals as well. However, when I changed from int to long this error appeared. I've looked for solutions and none works for me. Could anyone please take a look?

For reference, the txt file contains

95,0
1,0
1,0

The error "Exception in thread "AWT-EventQueue-0" java.util.InputMismatchException" comes up at line 74, which is

top3[i] = scanner.nextLong();

despite the fact that top3 is declared as 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;

}

}
Guido
  • 1
  • 1
  • A `long` is an integer type with more bits (64 vs 32 for `int`) so it can capture larger magnitudes - it is not a floating point type. For the latter you will want to use `float` or `double` and use the appropriate method in `Scanner` to read that type. I don't know if the `Scanner` will treat the commas as decimal points - that may be covered by your default `Locale` though. – vsfDawg Aug 05 '21 at 18:52
  • ok i'm an idiot thanks – Guido Aug 05 '21 at 18:55
  • top3[i] = scanner.nextDouble(); gives me the same error – Guido Aug 05 '21 at 19:10
  • Try replacing the comma (,) with a period (.). – vsfDawg Aug 05 '21 at 19:12
  • i've messed around a bit and it seems the software works fine with "," but as soon as "." are found it stops working. Issue is the software uses "." when it rewrites the file – Guido Aug 05 '21 at 19:20

1 Answers1

-1

You get the InputMismatchException exception because you are trying to read the string 95,0 as a long value. But a long number does not have a , character in it. As you used pw.println(top32[i]); previously, where top32[] is a double array, it was written as a double value in the file, with the , character.

Depending on what you want to do you can change it in a way that the value is written in the file as a long/int value by casting the value before writing it to the file or you can use nextDouble() to read the value as a double value and then cast it to long/int where needed.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • top3[i] = scanner.nextDouble(); gives me the same error – Guido Aug 05 '21 at 19:10
  • You might need to check and/or set the locale of the `Scanner` object. The locale which is used by the `PrintWriter` uses `,` as a decimal separator, but the `Scanner` object might use one which uses `.` as a decimal separator. See https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#localized-numbers for this issue. – Progman Aug 05 '21 at 19:20