1

Following is my code that I am working on for a school project. It does ok up until I try to read the animal.txt file. Can someone please tell me what I am doing wrong? I am attaching my compilation error as an image. Thanks in advance.

[input error image1

   package finalproject;

      //enabling java programs
      import java.util.Scanner;
      import javax.swing.JOptionPane;

import java.io.FileInputStream; import java.io.IOException;

public class Monitoring {

public static void choseAnimal() throws IOException{
    FileInputStream file = null;
    Scanner inputFile = null;
    System.out.println("Here is your list of animals");
     file = new FileInputStream("\\src\\finalproject\\animals.txt");
     inputFile = new Scanner(file);

    while(inputFile.hasNext())
    {
        String line = inputFile.nextLine();
        System.out.println(line);
    }
}

 public static void choseHabit(){
System.out.println("Here is your list of habits");

}


public static void main(String[] args) throws IOException{
    String mainOption = ""; //user import for choosing animal, habit or exit
    String exitSwitch = "n"; // variable to allow exit of system
    Scanner scnr = new Scanner(System.in); // setup to allow user imput




    System.out.println("Welcome to the Zoo");
    System.out.println("What would you like to monitor?");
    System.out.println("An animal, habit or exit the system?");
    mainOption = scnr.next();
    System.out.println("you chose " + mainOption);
    if (mainOption.equals("exit")){
    exitSwitch = "y";
    System.out.println(exitSwitch);
    }
    if (exitSwitch.equals( "n")){
        System.out.println("Great, let's get started");
    }
        if (mainOption.equals("animal")){
            choseAnimal();

        }
        if (mainOption.equals("habit")) {
            choseHabit();

        }

    else {
        System.out.println("Good bye");
    }

}

}

2
  • Don't, ever reference src in your code, it won't exist once the program is built and packaged. Netbeans automatically packages everything in the src directory into the result jar file Commented Oct 17, 2017 at 21:23
  • You also can't treat a resource of this type as a file, you need to use Class#getResource or Class#getResourceAsStream to be able to read it Commented Oct 17, 2017 at 21:23

2 Answers 2

1

\\src\\finalproject\\animals.txt suggests that the file is an embedded resource.

First, you should never reference src in you code, it won't exist once the program is built and package.

Secondly, you need to use Class#getResource or Class#getResourceAsStream in order to read.

Something more like...

//file = new FileInputStream("\\src\\finalproject\\animals.txt");
//inputFile = new Scanner(file);

try (Scanner inputFile = new Scanner(Monitoring.class.getResourceAsStream("/finalproject/animals.txt"), StandardCharsets.UTF_8.name()) {
    //...
} catch (IOException exp) {
    exp.printStackTrace();
}

for example

Now, this assumes that file animals.txt exists in the finalproject package

Sign up to request clarification or add additional context in comments.

Comments

0

The error message clearly shows that it can't find the file. This means there's two possibilities:

  1. File does not exist in the directory you want
  2. Directory you want is not the directory you have.

I would start by creating a File object looking at "." (current directory) to and printing that to see what directory it looks by default. You may need to hard code the file path, depending on what netbeans is using for a default directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.