0

I am trying to use a text file to input various parameters into an array and then use the array to create an object. Each lien in the text file has the Strings separated by commas for each object and separate lines for individual objects.

I cannot seem to figure out what I am doing wrong, and I keep receiving this error when i try to call the method to load the file: "Exception in thread "main" java.io.FileNotFoundException: studentData.txt (The system cannot find the file specified)"

this is my method:

    public void loadStudent() throws FileNotFoundException{

    File inputFile = new File("studentData.txt");
    Scanner input = new Scanner(inputFile);
    try{
    while(input.hasNext()){
        String info = input.nextLine();
        String elements[] = info.split(" , ");
        String fName = elements[0];
        String lName = elements[1];
        String phone = elements[2];
        String address = elements[3];
        double gpa = Double.parseDouble(elements[4]);
        String major = elements[5];
        Student student = new Student(fName, lName, phone, address, 
                                          gpa, major);
        addStudent(student);
        input.nextLine();
        count++;
    }
    input.close();

    }catch(FileNotFoundException e){
        e.printStackTrace();
    }
}
8
  • 1
    The doesn't exist where you think it is. Based on you code, it should be in the same directory from which the code is been executed Commented Dec 17, 2015 at 3:41
  • so you're saying the file name is incorrect?
    – acehamma
    Commented Dec 17, 2015 at 3:44
  • I'm say, where you think the file you're looking for is, it isn't. Try adding System.out.println(new File(".").getCanonicalPath()); or System.out.println(System.getProperty("user.dir")); to your code, this will tell where your program is been executed from, the file should reside within that directory Commented Dec 17, 2015 at 3:47
  • thank you, i just tried this and i found where the file is located but it wont allow me to put that location in, i am receiving a red underline that says "invalid escape sequence(valid ones are...etcetc)"
    – acehamma
    Commented Dec 17, 2015 at 3:55
  • No, don't use the path, place you file in that location and Java will read it Commented Dec 17, 2015 at 3:56

2 Answers 2

1

Okay, you have two choices...

Option #01...

You can include the file inside the project itself, by placing the file inside the src directory. This will embed the file in your program, making it readonly

Project Structure

(Yes, I know I'm using Netbeans, the concept is the same in Eclipse)

As you can see the studentData.txt is in the myawesomeproject, along side my Main class. Where in your src you place the does matter, so beware of that, if the file is NOT in the same package, then you will need to provide a relative or fully qualified path to it.

Next, we use Scanner input = new Scanner(getClass().getResourceAsStream("studentData.txt")) to open the file, for example...

package myawesomeproject;

import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        try {
            loadStudent();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public void loadStudent() throws FileNotFoundException {
        try (Scanner input = new Scanner(getClass().getResourceAsStream("studentData.txt"))) {
            while (input.hasNext()) {
                String info = input.nextLine();
                System.out.println(info);
                String elements[] = info.split(" , ");
                String fName = elements[0];
                String lName = elements[1];
                String phone = elements[2];
                String address = elements[3];
                double gpa = Double.parseDouble(elements[4]);
                String major = elements[5];

            }
        }
    }
}

Which in my example, prints...

B1 , B2 , B3 , B4 , 0 , B6

(which is the contents of my file)

Option #02...

You place the file in the "working" directory, this is the context from which the program is executed, typically, this is the project directory (the directory which contains the src directory)

enter image description here

Then you can use Scanner input = new Scanner(new File("studentData.txt")) to open it, for example...

package myawesomeproject;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        try {
            loadStudent();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public void loadStudent() throws FileNotFoundException {
        try (Scanner input = new Scanner(new File("studentData.txt"))) {
            while (input.hasNext()) {
                String info = input.nextLine();
                System.out.println(info);
                String elements[] = info.split(" , ");
                String fName = elements[0];
                String lName = elements[1];
                String phone = elements[2];
                String address = elements[3];
                double gpa = Double.parseDouble(elements[4]);
                String major = elements[5];

            }
        }
    }
}

Which outputs the same thing as above, since I used the same file and just moved it for the example

This does assume you've not changed the "working" directory. If you can test where the working directory is by using System.out.println(System.getProperty("user.dir"));, you would then move your file to this location

3
  • thank you i believe this worked, i am no longer receiving that error message, except now when i try to execute the methods i am getting this: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at finalProject.PeopleBag.loadStudent(PeopleBag.java:52) at finalProject.App.main(App.java:17)
    – acehamma
    Commented Dec 17, 2015 at 4:32
  • Then the way you are parsing the file is wrong, two things come to mind, perhaps String elements[] = info.split(" , "); should be String elements[] = info.split(","); and you're also double reading the file, you have two nextLine calls within your while-loop Commented Dec 17, 2015 at 4:34
  • thank you so much i fixed both of those things and now it is working, you are my hero
    – acehamma
    Commented Dec 17, 2015 at 4:43
0

The FileNotFoundException that you are getting indicates what the name implies: the JVM can't find the file at that location. Since the path you specified is relative (not absolute), chances are that it's not where you think it is (relative to the working directory you're in). To solve this you can either: a) update the file path to be correct relative to where your app is running or b) specify an absolute path to the input file

1
  • i have tried putting in the exact file location "C:\\Users\Andrew3\workspace\FinalProject\src\finalProject\studentData.txt" but i am receiving an error that says "invalid escape sequence(valid ones are ...etc)" i know that its probably something silly but i just can't seem to get it to work
    – acehamma
    Commented Dec 17, 2015 at 3:49

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.