“无法在Java中找到符号 - 方法hasNextLine()”错误

时间:2011-07-06 23:40:07

标签: java file-io methods

我正在尝试编写一个使用文件I / O对个性测试进行评分的程序。但是,当我得到这段代码时:

while (f.hasNextLine()) {

我收到标题中描述的错误。这是完整的代码。我之前导入了所有必要的I / O和实用程序,我认为我正确地声明了文件变量。我知道这种方法存在,但问题是什么?

import java.io.*;
import java.util.*;
public class PersonalityTest {
    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        String input = input();
        String output = output();
        results(input, output);
        System.out.println("Your results are located in the file you requested.");
    }

    public static String input() throws IOException {
        System.out.print("Input file name: ");
        String file = input.nextLine();
        File f = new File(file);
        System.out.println();

        while (!f.exists()) {
            System.out.print("File not found. Try again: ");
            file = input.nextLine();
            f = new File(file);
            System.out.println();
        }

        return file;
    }

    public static String output() {
        System.out.print("Output file name: ");
        String output = input.nextLine();
        return output;
    }

    public static void results(String input, String output) throws IOException {
        PrintStream write = new PrintStream(new File(output));
        File f = new File(input);

        while (f.hasNextLine()) {
            String name = f.nextLine();
            String type = "ESTJ";
            String answers = f.nextLine();
            char[] choices = new char[70];

            for (int i = 0; i <= 69; i++) {
                choices[i] = answers.charAt(i);
            }

            int aCount = 0;
            int bCount = 0;

            for (int i = 0; i <= 69; i+=7) {
                if (choices[i].toLowerCase == 'a') {
                    aCount+=1;
                }
                if (choices[i].toLowerCase == 'b') {
                    bCount+=1;
                }
            }

            int pct1 = (int)Math.round((bCount/(aCount+bCount))*100);

            if (pct1 > 50) {
                type.replace('E','I');
            }
            if (pct1 == 50) {
                type.replace('E','X');
            }

            int aCount2 = 0;
            int bCount2 = 0;

            for (int i = 2; i <= 69; i+=7) {
                if (choices[i].toLowerCase == 'a') {
                    aCount2+=1;
                }
                if (choices[i].toLowerCase == 'b') {
                    bCount2+=1;
                }
                if (choices[i+1].toLowerCase == 'a') {
                    aCount2+=1;
                }
                if (choices[i+1].toLowerCase == 'b') {
                    bCount2+=1;
                }
            }

            int pct2 = (int)Math.round((bCount2/(aCount2+bCount2))*100);

            if (pct2 > 50) {
                type.replace('S','N');
            }
            if (pct2 == 50) {
                type.replace('S','X');
            }

            int aCount3 = 0;
            int bCount3 = 0;

            for (int i = 4; i <= 69; i+=7) {
                if (choices[i].toLowerCase == 'a') {
                    aCount3+=1;
                }
                if (choices[i].toLowerCase == 'b') {
                    bCount3+=1;
                }
                if (choices[i+1].toLowerCase == 'a') {
                    aCount3+=1;
                }
                if (choices[i+1].toLowerCase == 'b') {
                    bCount3+=1;
                }
            }

            int pct3 = (int)Math.round((bCount3/(aCount3+bCount3))*100);

            if (pct3 > 50) {
                type.replace('T','F');
            }
            if (pct3 == 50) {
                type.replace('T','X');
            }

            int aCount4 = 0;
            int bCount4 = 0;

            for (int i = 6; i <= 69; i+=7) {
                if (choices[i].toLowerCase == 'a') {
                    aCount4+=1;
                }
                if (choices[i].toLowerCase == 'b') {
                    bCount4+=1;
                }
                if (choices[i+1].toLowerCase == 'a') {
                    aCount4+=1;
                }
                if (choices[i+1].toLowerCase == 'b') {
                    bCount4+=1;
                }
            }

            int pct4 = (int)Math.round((bCount4/(aCount4+bCount4))*100);

            if (pct4 > 50) {
                type.replace('J','P');
            }
            if (pct4 == 50) {
                type.replace('J','X');
            }

            write.println(name + ": [" + pct1 + ", " + pct2 + ", " + pct3 + ", " + pct4 + "] = " + type);

            write.close();
        }
    }
}

2 个答案:

答案 0 :(得分:5)

java.io.File没有hasNextLine()方法。这是java.util.Scanner中存在的方法。扫描程序有a constructor,它将File对象作为参数,并允许它使用指定的文件进行输入 - 您应该尝试使用该文件:

Scanner s = new Scanner(new File(input));

编辑:

那就是说,Scanner可能有点性能杀手(有点?)。使用BufferedReader及其readLine()方法将单行读入String对象然后手动解析String通常会更快。您可以从文件中获取BufferedReader,但有一些技巧:

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

答案 1 :(得分:0)

而不是使用hasNextLine()方法(在File对象中不存在),替代方法是使用像FileInputStream这样的输入流类访问您的文件,并将其包装在像{{{}这样的装饰器类中。 1}},这将允许您使用BufferedReader方法....

每行读取其内容