0

I have specific instructions for this program and I keep getting this statemen'Exception in thread "main" java.io.FileNotFoundException: Game.txt (The system cannot find the file specified) at java.io.FileInputStream.open0(Native Method).' Below I will attach my program but can someone please explain this in the simplest terms. It also won't repeat the process even if I enter yes so any comments will benefit but please keep it in the simplest terms!`

  int win;
  int totalComp = 0;
  int totalUser = 0;
  String replay;
  String rematch;
  String gameFile = "Game.txt";

  File file = new File(gameFile);  
  Scanner inputFile = new Scanner(file);

  if (!file.exists())
  {
     FileWriter fw  = new FileWriter (gameFile, true); //create file if doesn't exist
     System.out.println("Welcome to the game of rock, paper, scissors!"); 
  }
  else
  {
     replay = inputFile.nextLine();
     System.out.println("Welcome back! The score from last time was " + replay);

     inputFile.close();   //close the file
  }

  do
  {
     computer = compChoice(); //call to computer choice method
     user = userChoice(); // call to user choice method 
     win = winner(computer, user); // call to winner method

     if (win == 1)
        totalComp += 1;
     else if (win == 2)  
        totalUser += 1;

     System.out.println("The total score is User: " + totalUser + " and Computer: " + totalComp);
     Scanner keybd = new Scanner(System.in);
     System.out.println("Would you like to play again? Enter yes or no: ");
     rematch = keybd.nextLine();

  }while (rematch.equalsIgnoreCase("y"));

  System.out.println("Good game! Goodbye.");   //last statement

  PrintWriter outputFile = new PrintWriter(gameFile);   //open new or existing file
  outputFile.println("User: " + totalUser + " Computer: " + totalComp);
  outputFile.close();  //close file`
  • Usually this error is due to your not using the correct path String to the file. Understand that the file path is *relative to* the "user's working directory" as the [System Properties Java Tutorial](http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html) will tell you. – Hovercraft Full Of Eels Apr 08 '17 at 02:50
  • Is your file exists in the same directory of your code file? – Fady Saad Apr 08 '17 at 02:53
  • @user7790438: no, that won't help at all. The class loader isn't involved in this since he's not using System resources. Please don't confuse him with this nonsense. – Hovercraft Full Of Eels Apr 08 '17 at 03:00

0 Answers0