0

I am sure there is something really stupid I am missing, but for some reason I am getting an "Unhandled exception type FileNotFoundException" error even though my input.txt file is located within the package. Any help would be great!

Code for the scanner:

File file = new File("input.txt");
Scanner inputFile = new Scanner(file);
Company c = new Company();
String input = inputFile.nextLine();

Complete class code:

package SimpleJavaAssignment;

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

public class Company
{
  ArrayList<Department> deptList = new ArrayList<Department>();

  public Department checkDepartment(String name)
  {
    for(Department dept: deptList)
    {
      if(dept.getName().equals(name))
      {
      return dept;
      }
    }
    Department d = new Department(name);
    deptList.add(d);
    return d;
  }

  public static void main(String[] args)
  {

    System.out.println ("This program will compile and display the stored employee data.");


    File file = new File("input.txt");
    Scanner inputFile = new Scanner(file);
    Company c = new Company();
    String input = inputFile.nextLine();



    while(inputFile.hasNextLine() && input.length() != 0)
    {

      String[] inputArray = input.split(" ");
      Department d = c.checkDepartment(inputArray[3]);
      d.newEmployee(Integer.parseInt(inputArray[2]), inputArray[0] + " " + inputArray[1], d);
      input = inputFile.nextLine();
    }

    System.out.printf("%-15s %-15s %-15s %-15s %n", "DEPARTMENT", "EMPLOYEE NAME", "EMPLOYEE AGE",
          "IS THE AGE A PRIME");
    for(Department dept:c.deptList)
    {
      ArrayList<Employee> empList = dept.getEmployees();
      for(Employee emp: empList)
      {
      emp.printInfo();
      }
    }
  }    
}
Kevin Schultz
  • 886
  • 3
  • 20
  • 42
  • put it outside your src folder – Rod_Algonquin Jun 24 '14 at 22:48
  • If I put it outside the source folder, will I not have to code in the entire path, which would change when I share the program? – Kevin Schultz Jun 24 '14 at 22:49
  • Kevin, this might help: http://stackoverflow.com/questions/16313260/file-path-or-file-location-for-java-new-file . – KathyA. Jun 24 '14 at 22:52
  • You are referring to a bare file name with no path, so the program will look in the _current working directory_ which is _not necessarily_ the same as the directory where the jar file is. It depends on how exactly you invoke your java program. – Stephen P Jun 24 '14 at 22:55

2 Answers2

2

Your main problem has nothing to do with your looking in the right or wrong place for your file (although that might be a problem later), no your current problem is that you're not handling the exception that the compiler is complaining of. When reading a File and using it with a Scanner, you need to either put the code that throws the exception in a try/catch block or have your method throw the exception. If you haven't read the exception tutorial yet, please do yourself and us a favor and read it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Just fixed that too. Now dealing with visibility of the "Scanner inputFile;" which is no longer visible past the try/catch. – Kevin Schultz Jun 24 '14 at 22:57
1

new File("input.txt"); will seek for the file in the same location of the jar being executed. Since you state the file is in the same package of the class, you should use Class#getResource that returns an URL. Then, call URL#getFile to retrieve the String with the full path of the file.

File file = new File(getClass().getResource("input.txt").getFile());

Since you're executing this code in a static method, replace getClass by ClassName.class:

File file = new File(Company.class.getResource("input.txt").getFile());
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thanks for the response, I tried that and I get the following error. "Cannot make a static reference to the non-static method getClass() from the type Object" – Kevin Schultz Jun 24 '14 at 22:51