title: InputMismatchException in a scanner to get data from txt file
body: <p>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?</p>
  <p>For reference, the txt file contains</p>
  <pre><code>95,0
  1,0
  1,0
  </code></pre>
  <p>The error &quot;Exception in thread &quot;AWT-EventQueue-0&quot; java.util.InputMismatchException&quot; comes up at line 74, which is</p>
  <p><code>top3[i] = scanner.nextLong();</code></p>
  <p>despite the fact that top3 is declared as long.</p>
  <pre><code>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(&quot;iniziando a riscrivere il file&quot;);
              File file1 = new File(&quot;C:\\Users\\39340\\Desktop\\storageapp\\highscore&quot;, &quot;highscores.txt&quot;);
                          
              FileWriter fw = new FileWriter(file1, append);
              
              PrintWriter pw = new PrintWriter(fw);
              
              //le tre linee sotto cancellano tutto
              FileOutputStream writer = new FileOutputStream(&quot;C:\\Users\\39340\\Desktop\\storageapp\\highscore\\highscores.txt&quot;);
              writer.write((&quot;&quot;).getBytes());
              writer.close(); 
              
              //qui scrive il file nuovo
              for (int i = 0; i &lt; 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(&quot;iniziando a riscrivere il file&quot;);
              File file1 = new File(&quot;C:\\Users\\39340\\Desktop\\storageapp\\highscore&quot;, &quot;averagehighscore.txt&quot;);
                          
              FileWriter fw = new FileWriter(file1, append);
              
              PrintWriter pw = new PrintWriter(fw);
              
              //le tre linee sotto cancellano tutto
              FileOutputStream writer = new FileOutputStream(&quot;C:\\Users\\39340\\Desktop\\storageapp\\highscore\\averagehighscore.txt&quot;);
              writer.write((&quot;&quot;).getBytes());
              writer.close(); 
              
              //qui scrive il file nuovo
              for (int i = 3; i &lt; 6; i++) {
                  pw.println(top3[i]); 
              }
              
              pw.close();
              
          }
  
      public static double[] main(double punti, double avgPunti) throws IOException {
          
          //System.out.println(&quot;sono in writehighscore&quot;);
          
          //in teoria sta cosa guarda che non si siano fine e ne crea uno se non c'è
          File file = new File(&quot;C:\\Users\\39340\\Desktop\\storageapp\\highscore\\highscores.txt&quot;);
          //if (!file.exists()) {saveToFile(punti, true); System.out.println(&quot;ho scritot i punti nel file&quot;);}
                          
          //questa roba legge il file e lo mette in un array
          Scanner scanner = new Scanner(new File(&quot;C:\\Users\\39340\\Desktop\\storageapp\\highscore\\highscores.txt&quot;));
          
          for (int i = 0; i &lt; 3; i++) {
          
              top3[i] = scanner.nextLong(); //qui scrive
  
          } 
          
          double exTop = 0;
          double exSecondo = 0;
          //System.out.println(&quot;sto iniziando a controllare i valori con punti = &quot; + punti);
          if (top3[0] &lt; punti) {exTop =top3[0]; top3[0] = punti; newTop3 = true;} else {exTop =  punti;}
          if (top3[1] &lt; exTop) {exSecondo = top3[1]; top3[1] = exTop; newTop3 = true;} else {exSecondo = exTop;}
          if (top3[2] &lt; exSecondo) {top3[2] = exSecondo; newTop3 = true;}
          
          if (file.exists()) {RewriteFile(top3, true);}
          
          File file2 = new File(&quot;C:\\Users\\39340\\Desktop\\storageapp\\highscore\\averagehighscore.txt&quot;);
          Scanner scanner2 = new Scanner(new File(&quot;C:\\Users\\39340\\Desktop\\storageapp\\highscore\\averagehighscore.txt&quot;));
  
          for (int i = 3; i &lt; 6; i++) {
              
              top3[i] = scanner2.nextInt(); //qui scrive
              //System.out.println(&quot;sto scrivendo nella top3. qui vale&quot;+top3[i]);
              //System.out.println(&quot;2top3 &quot;+top3[i]);
          }
          
          double exAvgTop = 0;
          double exAvgSecondo = 0;
                  
          //System.out.println(&quot;sto iniziando a controllare i valori con punti = &quot; + punti);
          if (top3[3] &lt; avgPunti) {exAvgTop = top3[3]; top3[3] = avgPunti; newTop3 = true;} else {exAvgTop = avgPunti;}
  
          if (top3[4] &lt; exAvgTop) {exAvgSecondo = top3[4]; top3[4] = exAvgTop; newTop3 = true;} else {exAvgSecondo = exAvgTop;}
  
          if (top3[5] &lt; exAvgSecondo) {top3[5] = exAvgSecondo; newTop3 = true;}
  
          if (file.exists()) {AvgRewriteFile(top3, true);}
          
          if (newTop3 = true) {return top3;} else return null;
  
  }
  
  }
  </code></pre>