So basically the program calls to use a class type LinkedList to store lines from a text file. Course is a separate class I have so that is the type of LinkedList I'm using. Here is the txt file the program is reading from
CPSC141 Computer_Organization 3 D
CPSC130 Computer_ProgrammingI 3 B
Math140 Calculus_I 4 E
CPSC232 Assembler_Languages 3 B
CPSC131 Computer_ProgramingII 3 A
Math141 Calculus_II 4 B
Here is my code
File inFile = new File("courses.txt");
Scanner f = new Scanner(inFile);
while (f.hasNextLine())
{
Courses.add(f);
}
f.close();
The problem is I get an error message that says this
method.java.util.Collection.add(Course) is not applicable;
(argument mismatch; java.util.Scanner cannot be converted to Course);
I don't know how to convert the string from the txt file to type Course (my separate class)
在Java 8中,您可以简单地使用:
List<String> yourList = Files.readAllLines(Paths.get("courses.txt"));
f
is a Scanner
instance. Change it to the line that it read in following line.
//from
Courses.add(f);
//To
Courses.add(f.nextLine());
Right now the variable f is a Scanner instance. You can either convert it to a string with String.valueOf or toString() methods, or read the entire line as a string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.