简体   繁体   English

我不断收到找不到文件的错误

[英]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"? 例如“ c:\\ documents \\ file.txt”? If you are using windows go to properties, open general tab and see the file's location. 如果使用的是Windows,请转到属性,打开“常规”选项卡,然后查看文件的位置。

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 它将向您显示JVM希望在哪里找到文件,以及它是否也是您期望的文件夹,相应地放置文件或提供确切的位置

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 如果执行上述操作,您会看到文件位于C:\\ Harley \\ Test \\ strength.txt中,即:它期望文件位于项目基本目录中

[you can also check if your file is in the same location as your .class file is. [您还可以检查文件是否与.class文件位于同一位置。 In the bin directory. 在bin目录中。 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. 不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在尝试创建 XML 文件时不断出错 - I keep getting error while trying to create an XML file Java 加载文本文档并根据重复单词的数量对它们进行排名的程序 - 不断获取文件未找到错误 - Java Program that loads text documents and ranks them based on amount of repeated words - keep getting file not found error 我不断收到错误 incompatible type , Required android.widget.spinner, found int - I keep getting an error incompatible type , Required android.widget.spinner, found int 为什么在Android的ExifInterface上出现File not found错误? - Why am I getting File not found error for ExifInterface with Android? 在春季找不到错误文件 - getting error file not found in spring 尝试将图像添加到GUI,但不断出现错误“ javac” - Trying to add a image to a GUI but keep getting the error “javac” not found 尝试从文件中拆分行时,我不断收到IndexOutOfBounds错误 - I keep getting IndexOutOfBounds error when I try to split a line from a file 我不断收到404 tomcat错误 - i keep getting 404 tomcat error 我不断收到此错误int无法取消引用 - I keep getting this error int cannot be dereferenced 我一直得到一个“别无错误”的错误 - I keep getting an “else without if” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM