程序无法在同一文件夹中查找文件

时间:2016-11-20 00:43:53

标签: java

我必须根据学生收到的成绩来创建一个直方图,该直方图位于名为" midterm.txt。"的文本文件中。我已经发布了我到目前为止所提出的内容,但说实话,我不知道我是否在正确的道路上,因为我无法获得该计划跑来看看它的表现。为了解释我目前使用的思维过程,我用等级创建了几个列表,然后尝试读取文件中的每个整数,并将它读取的整数与我定义的int匹配。如果匹配,它将添加到该计数。在它下面我输出每个计数只是为了看它是否正常工作(除了程序没有运行。)如果我能确认它的计数,那么我将创建一个直方图。以下是我在日食中得到的错误。

import java.util.*;
import java.io.*;
public class classGrades {
    public static void main(String[] args) throws FileNotFoundException{
        File file = new File("midterm.txt");
        Scanner grades = new Scanner(file);
        int[] a = {71, 72, 73, 74, 75};
        int[] b = {76, 77, 78, 79, 80};
        int[] c = {81, 82, 83, 84, 85};
        int[] d = {86, 87, 88 , 89, 90};
        int[] e = {91, 92, 93, 94, 95};
        int[] f = {96, 97, 98, 99, 100};
        int one = 0;
        int two = 0;
        int three = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        while(grades.hasNextLine()){
            int grade = grades.nextInt();
            if(grade == a.length){
                one++;
            } else if(grade == b.length){
                two++;
            } else if(grade == c.length){
                three++;
            } else if(grade == d.length){
                four++;
            } else if(grade == e.length){
                five++;
            } else if(grade == f.length){
                six++;
            }
        }
        System.out.println(one);
        System.out.println(two);
        System.out.println(three);
        System.out.println(four);
        System.out.println(five);
        System.out.println(six);
    }
}

我在控制台中遇到的错误。

Exception in thread "main" java.io.FileNotFoundException: midterm.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at classGrades.main(classGrades.java:6)

0 个答案:

没有答案