[英]“Cannot Find Symbol - method hasNextLine()” error in Java
I am trying to write a program that uses file I/O to score personality tests.我正在尝试编写一个使用文件 I/O 对性格测试进行评分的程序。 However, when I get to this code:但是,当我看到这段代码时:
while (f.hasNextLine()) {
I get the error described in the title.我收到标题中描述的错误。 Here is the code in its entirety.这是完整的代码。 I import all of the necessary I/O and utilities prior and I think I am declaring the file variable correctly.我之前导入了所有必要的 I/O 和实用程序,并且我认为我正确地声明了文件变量。 I know that this method exists, but what is the problem?我知道这个方法是存在的,但是有什么问题呢?
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();
}
}
}
java.io.File does not have a hasNextLine()
method. java.io.File没有hasNextLine()
方法。 That's a method that exists in java.util.Scanner .这是java.util.Scanner中存在的一种方法。 Scanner has a constructor that takes a File object as an argument and allows it to use the specified file for input - you should probably try using that one: Scanner 有一个构造函数,它将 File object 作为参数,并允许它使用指定的文件进行输入——你应该尝试使用那个文件:
Scanner s = new Scanner(new File(input));
EDIT:编辑:
That said, Scanner can be a bit of a performance killer (a bit?).也就是说,Scanner 可能有点像性能杀手(有点?)。 It is often faster to use a BufferedReader and its readLine() method to read a single line into a String object and then parse the String manually.使用BufferedReader及其readLine()方法将单行读入字符串 object 然后手动解析字符串通常更快。 You can get a BufferedReader from a File with a bit of trickery:您可以通过一些技巧从文件中获取 BufferedReader:
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
instead of using hasNextLine()
method, which is non-existent in a File object, an alternative would be to access your file using an inputstream class like FileInputStream
and wrapped inside a decorator class like BufferedReader
, which would allow you to read its contents per line, using readLine()
method....而不是使用文件 object 中不存在的hasNextLine()
方法,另一种方法是使用输入流 class 访问您的文件,如FileInputStream
并包装在装饰器 ZA2F2ED4F8EBC2CBB4C21A29DC406 中,这将允许您读取其BufferedReader
内容。行,使用readLine()
方法....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.