简体   繁体   中英

I keep getting a file not found error

Here's the prompt:

Write a program that reads a file containing two columns of floating-point numbers. Prompt the user for the file name. Print the average of each column.

My problem is that my program can't find my input file.

EDIT: I am no longer getting the "File not found." error but rather my program doesnt doi anything after the file is found...

Here's my code:

 public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter file name.");
    String file = in.next();
    try {
        Scanner inFile = new Scanner(new File(file));
        int count = 0;
        float average1 = 0;
        float average2 = 0;
        while (inFile.hasNextFloat()) {
            String str = inFile.nextLine();
            Scanner line = new Scanner(str);
            line.useDelimiter(" ");
            average1 = Float.parseFloat(line.next());
            average2 = Float.parseFloat(line.next());
            average1 += in.nextFloat();
            average2 += in.nextFloat();
        }
        System.out.println("The average of the first column: " + average1 / count);
        System.out.println("The average of the second column: " + average2 / count);
    } catch (FileNotFoundException e) {
        System.out.println("File not found.");
    }
}

Probably you are giving a wrong path. Are you typing the full path to the file? like "c:\\documents\\file.txt"? If you are using windows go to properties, open general tab and see the file's location.

Place the file in your project base directory

Also Do this

System.out.println(openFile.getAbsolutePath());

It will show you where JVM expects to find the file and whether it is the folder you expect as well, Accordingly place the file or give the exact location

If you do the above , you see that your file is in say C:\\Harley\\Test\\strength.txt ie: it is expecting the file in the project base directory

[you can also check if your file is in the same location as your .class file is. In the bin directory. Thereby you understand better]

sample code snippet:

String file = "filename.txt";
 File openFile = new File(file);
        Scanner inFile = new Scanner(openFile);

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

Here is the problem:

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

public class ExampleClass {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter file name.");

    // you put 'in.next();' put 'in.nextLine()'.
    String file = in.nextLine();
try {
    Scanner inFile = new Scanner(new File(file));
    int count = 0;
    float average1 = 0;
    float average2 = 0;
    while (inFile.hasNextFloat()) {
        String str = inFile.nextLine();
        Scanner line = new Scanner(str);
        line.useDelimiter(" ");
        average1 = Float.parseFloat(line.next());
        average2 = Float.parseFloat(line.next());
        average1 += in.nextFloat();
        average2 += in.nextFloat();
    }
    System.out.println("The average of the first column: " + average1 / count);
    System.out.println("The average of the second column: " + average2 / count);
} catch (FileNotFoundException e) {
    System.out.println("File not found.");
}
}

}

If it is still not working are you entering the path using '\\' ? if you are then you have to enter the path like '/' example:

C:\Users\

Would produce an error.

C:/Users/

Would not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM