我有一个Java程序,允许教师加载并列出GPA高于用户输入的学生。该程序有三个类:一个类为每个学生提供获取和设置方法(Student
(,一个类提供教师可以选择的四个不同选项(Communicator
(,以及一个类调用Communicator
类启动(HW4
(。Communicator
类中的四个选项包括1(使用文本文件加载学生列表,2(显示GPA高于用户输入的GPA的学生,3(打印已加载到程序中的文件的加载日志,以及4(退出程序。
我有每个程序的所有基本部分在工作。然而,我很难识别程序是否已经加载了文件,它应该回复";文件已加载"加载到程序中的每个文件都被添加到名为"树集"的树集中;logHistory;我尝试使用if语句,如果日志历史记录包含输入,该语句基本上会打印出文件已经加载。然而,它只是打印出";[文本文件]加载成功!文件已加载"即使我已经输入过一次。关于如何解决这个问题有什么想法吗?
这是我为每个类编写的代码:
HW4.java:
public class HW4 {
public static void main(String[] args) throws Exception {
//Start communicator
Communicator.start();
}
}
Student.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 + '}';
}
}
通讯器.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 studentsn>>");
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
添加条件,检查输入文件名是否已存在于日志历史记录中。你可以随心所欲。
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")){
if(!logHistory.contains(input)) {//check if the input file does not exist
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!");
}
}
else {//the input file exist in logHistory
System.out.println("File already loaded!");
}
}
}
您需要检查input
文件之前是否已经加载。要做到这一点,您需要检查input
是否已经在您的加载文件列表中。以下内容应该有效:
import java.util.*;
import java.io.*;
public class Communicator {
static List<String> loadedStudentLists;
static LinkedList<Student> studentList;
public static void start() throws Exception{
loadedStudentLists = new ArrayList<>();
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")){
if (loadedStudentLists.contains(input)) {
System.out.println("File already loaded!");
} else {
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!");
loadedStudentLists.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 studentsn>>");
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<loadedStudentLists.size();i++) {
String log = loadedStudentLists.toString().replace("[", "").replace("]", "");
System.out.println(log);
}
return;
}
public static void main(String[] args) throws Exception {
start();
}
}