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;
}
}