【发布时间】:2021-11-12 21:45:12
【问题描述】:
我有一个 Java 程序,允许教师加载并列出 GPA 高于用户输入的学生。该程序具有 3 个类:一个为每个学生提供 get 和 set 方法 (Student),一个提供四种不同的选项供教师选择 (Communicator),以及一个调用 Communicator 的类类启动(HW4)。 Communicator 类中的四个选项包括 1) 使用文本文件加载学生列表,2) 显示 GPA 高于用户输入的 GPA 的学生,3) 打印已加载到程序,以及 4) 退出程序。
每个程序的所有基本部分都可以正常工作。但是,我在识别文件是否已加载时遇到困难,它应该回复“文件已加载!”加载到程序中的每个文件都被添加到名为“logHistory”的 TreeSet 中,我尝试使用 if 语句,如果日志历史记录包含输入,则基本上将打印出文件已经加载。但是,它只是打印出“[文本文件]已成功加载!文件已加载!”即使我已经输入过一次。有关如何解决此问题的任何想法?
这是我每个类的代码:
HW4.java:
public class HW4 {
public static void main(String[] args) throws Exception {
//Start communicator
Communicator.start();
}
}
学生.java:
public class Student {
private String firstName;
private String lastName;
private float gpa;
public Student(String firstName, String lastName, float gpa){
this.firstName = firstName;
this.lastName = lastName;
this.gpa = gpa;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public float getGPA() {
return gpa;
}
public void setGPA(float gpa) {
this.gpa = gpa;
}
@Override
public String toString() {
return "Student{" + "First Name = " + firstName + ", Last Name = " + lastName + ", GPA = " + gpa + '}';
}
}
Communicator.java:
import java.util.*;
import java.io.*;
public class Communicator {
static Set<String> logHistory;
static LinkedList<Student> studentList;
public static void start() throws Exception{
logHistory = new TreeSet<String>();
studentList = new LinkedList<Student>();
System.out.println("Hello professor. How can I help you?");
displayMainMenu();
}
public static void displayMainMenu() throws Exception {
String input;
Scanner in = new Scanner(System.in);
do {
System.out.printf("1: Load student list.\n");
System.out.printf("2: Display students by gpa.\n");
System.out.printf("3: Print load log.\n");
System.out.printf("4: Exit.\n");
System.out.printf(">> ");
input = in.next();
if(invalidInput(input)) {
continue;
}
switch(input) {
case "1":
OptionOne();
break;
case "2":
OptionTwo();
break;
case "3":
printLog();
break;
case "4":
System.out.println("Good bye! :)");
return;
}
} while (true);
}
public static boolean invalidInput(String input) {
if(!(input.charAt(0) == '1' || input.charAt(0) == '2' || input.charAt(0) == '3' || input.charAt(0) == '4')) {
System.out.println("Error! Not an option!");
return true;
}
return false;
}
public static void OptionOne() throws FileNotFoundException {
Scanner in = new Scanner(System.in);
System.out.print("Enter the name of the class list or press 'B' to go back to the main menu.\n>>");
String input = in.next();
if (!input.equals("B")){
File File1 = new File("C:\\Users\\myName\\eclipse-workspace\\HW4\\" + input);
if(File1.exists()) {
Scanner inFile = new Scanner(File1);
while(inFile.hasNext()) {
String inFirstName = inFile.next();
String inLastName = inFile.next();
float inGPA= Float.parseFloat(inFile.next());
Student stu = new Student(inFirstName, inLastName, inGPA);
studentList.add(stu);
}
System.out.println(input + " loaded successfully!");
logHistory.add(input);
}
else {
System.out.println("File does not exist!");
}
}
}
public static void OptionTwo() {
Scanner in = new Scanner(System.in);
System.out.print("Type in a GPA for which you would like to display students\n>>");
float user_input = in.nextFloat();
System.out.println("Hey");
for(Student myStudent : studentList) {
if(user_input <= myStudent.getGPA()) {
System.out.println(myStudent);
}
}
}
public static void printLog() {
for(int i=0; i<logHistory.size();i++) {
String log = logHistory.toString().replace("[", "").replace("]", "");
System.out.println(log);
}
return;
}
public static void main(String[] args) throws Exception {
start();
}
}
还有一个我正在使用的文本文件的示例:
Madeline Holmes 0.3966
Cassandra Wilkerson 1.2734
Sherman Tucker 0.8281
Ray Owen 1.6671
Irving Klein 1.3626
Chris Young 0.801
Ora Reynolds 0.6448
Charlie Collins 0.4683
Wayne White 0.0056
Bobby Price 2.9535
Troy Keller 1.9381
Herbert Lloyd 3.1048
Anita Robinson 2.6867
Lorene Brady 2.8133
Wendy Coleman 0.9067
Rochelle Fernandez 2.5683
Wanda Clayton 1.0686
Jamie Simon 0.6709
Maggie Gonzalez 3.9742
Felicia Powers 1.7226
Allison Summers 3.4843
Kristopher Maxwell 1.4895
Donna Rose 1.519
Sue Alvarado 0.0465
Pearl Carlson 3.035
Abraham Rodgers 2.3123
Billie Doyle 0.9774
Edna Fitzgerald 3.8697
Christie Day 0.8813
Terri Pena 3.9045
Maria Carpenter 2.2858
Jamie Manning 0.9225
Rose Bridges 1.5402
Zachary Parker 3.723
Julie Rowe 0.9275
Javier Warren 1.1316
Cora Townsend 0.6736
Yvette Rodriguez 0.2977
Gloria Henderson 0.4383
Rafael Weaver 1.9113
Kim Barrett 0.1046
Orlando Dunn 3.0004
Stuart Terry 0.4609
Elaine Davis 2.3482
Simon Jones 3.5721
Faith Rivera 1.1192
Jodi Kelly 1.9566
Loren Spencer 0.7206
Ignacio Hicks 3.3818
Johanna James 0.3527
Arturo Huff 2.3178
Arnold Barker 0.7946
Marc Weber 1.1336
Joy Bush 0.6794
Brenda Hubbard 3.0953
Carroll Romero 2.5379
Ann Moore 2.1482
Archie Duncan 2.897
Eddie Fields 3.1792
Jan Andrews 3.4771
Della Waters 2.0199
Nadine Pierce 1.3615
Carlos Ortega 1.4948
Toni Ross 0.1255
Israel Carson 0.1968
Essie Morton 3.561
Wesley Poole 1.3881
Philip Benson 2.071
Jonathon Morgan 3.4305
Theresa Hunt 1.7487
Jaime Ruiz 2.5617
Maxine Evans 1.8658
Meghan Hopkins 0.4686
Darnell Hines 2.8667
Rhonda Garner 2.9759
Blake Mills 2.0325
Cedric Jennings 2.2025
Lisa Miles 3.9877
Steve Ball 1.1752
Sherri Gutierrez 0.3944
Rudolph Cross 0.3921
Mario Cunningham 1.5849
Eileen Gonzales 1.2047
Tami Malone 0.1306
Nathan Becker 1.1747
Brett Alexander 1.5998
Colleen Cole 2.7064
Ivan Harris 1.539
Tiffany Gibbs 0.7235
Judith Kim 2.8998
Christy Blake 0.7685
Gerardo Hammond 0.88
Dwight Bailey 0.8722
Brian Santiago 2.634
Andrew Watkins 3.8533
Garrett Fox 1.1582
Claire Graham 0.5412
Hubert Mitchell 1.0085
Grant Bowman 2.8718
Yolanda Gomez 1.2913
【问题讨论】:
-
简单的方法是保存一个加载路径列表。在类中有像 Collection
loaded = new HashSet() 这样的东西来跟踪这些东西,并在加载完成后将文件路径添加到列表中。然后只需检查是否加载了.contains(path) 来检测加载的数据。 -
你能给我举个例子吗?
标签: java linked-list treeset