-2
java.io.FileNotFoundException: Monsters.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)

at MonsterApp.main(MonsterApp.java:28)

This is the error that i am receiving while running my application.. I cant seem to fix the code. From looking at this what would be the most likely to happen error.


This is the code from the app that im running

__ public class MonsterApp {

//A list of all monsters read in.
private static ArrayList<Monster> monsters = new ArrayList<Monster>();

/**
 * The main method
 * @param args
 */
public static void main(String[] args) {
    //Try for IOExceptions -- called when file cannot be found
    try {
        /* REQUIREMENT : FILE INPUT */
        //Connect to the monsters.txt file
        BufferedReader br = new BufferedReader(new FileReader("Monsters.txt"));

        String line = "";

        //Read in each line
        while((line = br.readLine()) != null) {
            //Split the line
            String[] parts = line.split(",");

            //Try to create monsters from the file details
            try {
                //Swtich which kind of monster it is
                switch(parts[0]) {
                    case "Chimera":
                        /* REQUIREMENT : POLYMORPHISM */
                        //Add chimera to monster list
                        monsters.add(new Chimera(Monster.SIZE.indexOf(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]), 
                                parts[4], Integer.parseInt(parts[5])));
                        break;
                    case "Hydra":
                        /* REQUIREMENT : POLYMORPHISM */
                        //Add hydra to monster list
                        monsters.add(new Hydra(Monster.SIZE.indexOf(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]), 
                                parts[4], Integer.parseInt(parts[5]), new Toy(parts[6], parts[7])));
                        break;
                }
            } catch (MonsterException ex) {
                //Something is wrong with the monster, output the message
                System.out.println(ex.getMessage());
            }
        }

        //Loop through all the monsters
        for(Monster mon : monsters) {
            //Output the toString result of each monster
            System.out.println(mon.toString());

            //Call the scare method
            mon.scare();

            //Call the sleep method
            mon.sleep();
        }

        //Close the file
        br.close();

        //Open the runcount file
        BufferedReader br2 = new BufferedReader(new FileReader("runcount.txt"));

        //Read the number of runs
        int count = Integer.parseInt(br2.readLine());

        //Close the file
        br2.close();

        //Get the file
        File file = new File("runcount.txt");

        //Open for writing
        PrintWriter pw = new PrintWriter(file);

        //Increment the count
        count++;

        /* REQUIREMENT : FILE OUTPUT */
        //Output to the file
        pw.println(count);

        //Close the file
        pw.close();

        //Output the run count information
        System.out.println("\nThis program has been run " + count + " times");
    } catch (IOException e) {
        //A file reading/writing error has occurred.
        e.printStackTrace();
    }   
}

}

hd10
  • 1
  • 2
  • 1
    Can you share the code please? – Mureinik Apr 28 '15 at 22:51
  • 1
    FileNotFoundException usually occurs when you point to a file that does not exist or when the file cannot be accesed. Seems that this is happening because you provided the file name without the path. So I'd suggest to provide the right path, like c:/mydocs/Monster.txt. By the way, it is kind of difficult to help you without any source code, try to provide your source code to be able to help you. – Marcelo Tataje Apr 28 '15 at 22:52
  • 1
    It literally means it cannot find your file – Transcendence Apr 28 '15 at 22:52
  • 1
    Most likely problem is that you made a mistake in file path. Try to pass absolute file path. – dmitrievanthony Apr 28 '15 at 22:52
  • 1
    possible duplicate of [Java - FilenotfoundException for reading text file](http://stackoverflow.com/questions/16891760/java-filenotfoundexception-for-reading-text-file) – ericbn Apr 28 '15 at 22:55
  • 1
    Please post code so that we can help you – sazzy4o Apr 28 '15 at 23:10
  • I did, im not sure how to update the commentators that i did. – hd10 Apr 29 '15 at 00:39

1 Answers1

0

Change your code new FileReader(name) to new FileReader(new File(name).getAbsoluteFile()) and your exception will tell you where it tries to find the file. It is most likely not the place where you think it is.

eckes
  • 10,103
  • 1
  • 59
  • 71
  • i think i have found the answer to my Exception problem. however now i have a problem when running the app with the Increment when its count++; MonsterApp.java:91: error: ';' expected count++; ^ 1 error this is the error im receiving – hd10 Apr 29 '15 at 01:00
  • Sounds like a compile error, not a runtime error. Your code above looks correct, is it the code you try to compile? Which one is line:91. – eckes Apr 29 '15 at 01:10