0

I'm trying to make a simple highscore system for a minesweeper game. However i keep getting a file not found exception, and i've tried to use the full path for the file aswell.

package minesweeper;

import java.io.*;
import java.util.*;

public class Highscore{

 public static void submitHighscore(String difficulty) throws IOException{
  int easy = 99999;
  int normal = 99999;
  int hard = 99999;
  //int newScore = (int) MinesweeperView.getTime();
  int newScore = 10;
  File f = new File("Highscores.dat");

  if (!f.exists()){
   f.createNewFile();

  } 
  Scanner input = new Scanner(f); 
  PrintStream output = new PrintStream(f);

  if (input.hasNextInt()){
   easy = input.nextInt();
   normal = input.nextInt();
   hard = input.nextInt();
  }

  output.flush();



  if(difficulty.equals("easy")){
   if (easy > newScore){
   easy = newScore;
   }
  }else if (difficulty.equals("normal")){
   if (normal > newScore){
   normal = newScore;
   }
  }else if (difficulty.equals("hard")){
   if (hard > newScore){
   hard = newScore;
   }
  }
  output.println(easy);
  output.println(normal);
  output.println(hard);

 }

//temporary main method used for debugging

 public static void main(String[] args) throws IOException {
  submitHighscore("easy");
 }  

}
Jonathon Bolster
  • 15,811
  • 3
  • 43
  • 46
TBH
  • 23
  • 1
  • 3

3 Answers3

1

You don't reveal on which line of code the exception is emitted. (Note: not posting all information you have about the problem reduces your chances of getting useful answers.)

However, my hunch is that it comes from the second call shown below, in which case the problem lies in trying to open the file twice:

Scanner input = new Scanner(f); 
PrintStream output = new PrintStream(f);
Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • That might be it, thank you! So i'll have to on somehow close the Scanner, before opening the prinstream? – TBH Jan 14 '11 at 09:56
  • @Troels, exactly. Closing is easy - see [`Scanner.close()`](http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html#close%28%29) :-) – Péter Török Jan 14 '11 at 10:01
0

Have you checked that the file exists and you have access rights for it?

Navi
  • 8,580
  • 4
  • 34
  • 32
0

Have you tried this?

if(f.isFile()) 
   System.out.println("Yes, we have a file");

if(f.canWrite()) 
   System.out.println("Yes, we have can write to the file");
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
  • Both returns true. Apperently it's just reading from the file that doesn't work, but when debugging, it shows the filenotfoundexception for both the scanner and printstream, which confused me. Thanks for he answers, but is there any way i can make the reading from the file work? – TBH Jan 14 '11 at 09:50
  • Have a look at http://stackoverflow.com/questions/1160050/java-i-o-streams-what-are-the-differences/1674000#1674000. Follow the link. Also: http://www.java-tips.org/java-se-tips/java.io/how-to-read-file-in-java.html – Shervin Asgari Jan 14 '11 at 09:57