I am trying to write a program that uses file I/O to score personality tests. 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 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;
开发者_Go百科 }
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. That's a method that exists in 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 s = new Scanner(new File(input));
EDIT:
That said, Scanner can be a bit of a performance killer (a bit?). 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. You can get a BufferedReader from a File with a bit of trickery:
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....
精彩评论