我正在处理多个类的classroll程序。在classroll类中,我有一个构造方法" ClassRoll(String f){"这是假设....
从输入文件f中读取类卷数据,为每个学生创建Student对象,并将它们添加到学生的ArrayList中。 输入文件包含第一行的课程标题。每个学生的数据显示在一个单独的行上,该行包括名字,姓氏,得分1,得分2和得分3,由至少一个空格分隔。
下面我将展示classroll类和我尝试调用该构造函数的主要方法。每次运行程序时,我都会收到来自该构造函数的错误。我需要有关如何正确创建构造函数的帮助以执行它需要执行的操作,因为我尝试它并不起作用。任何帮助都将不胜感激,谢谢。
public class ClassRoll {
private ArrayList<Student> students = new ArrayList<Student>();
private String title;
private String filename = "data.txt";
ClassRoll(String f) {
Scanner kb = new Scanner(System.in);
String inpFileName = kb.next();
File inpFile = new File(inpFileName);
boolean firstline = true;
while(kb.hasNextLine()){
if(firstline){
title = kb.nextLine();
firstline = false;
}
else{
String fName = kb.nextLine();
String lName = kb.nextLine();
int score1 = kb.nextInt();
int score2 = kb.nextInt();
int score3 = kb.nextInt();
students.add(new Student(fName, lName));
}
}
}
void Remove() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
students.remove(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
void Display() {
DecimalFormat fmt = new DecimalFormat("0.00");
System.out.println("\t\t\t" + title);
double classAverage = 0.0;
for (int i = 0; i < students.size(); i++) {
Student s = (Student) students.get(i);
System.out.print(s.toString());
System.out.println("\t" + fmt.format(s.getAverage()));
classAverage = classAverage + s.getAverage();
}
System.out.println("\t\t\t" + fmt.format(classAverage /
students.size()));
}
void Add() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
System.out.println("What is the Student's second score?");
int score2 = kb.nextInt();
System.out.println("What is the Student's third score?");
int score3 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
System.out.println("Student already in class");
} else {
students.add(s);
}
}
}
void changeScore1() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
s.setScore1(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
void changeScore2() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
s.setScore2(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
void changeScore3() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
s.setScore3(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
public void find() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0){
System.out.println(s);
}
}
}
public void sortAverage() {
for (int i = 0; i < students.size() - 1; i++) {
for (int j = i + 1; j < students.size(); j++) {
Student s1 = (Student) students.get(i);
Student s2 = (Student) students.get(j);
if (s1.getAverage() < s2.getAverage()) {
students.set(i, s2);
students.set(j, s1);
}
}
}
}
public void sortNames() {
for (int i = 0; i < students.size() - 1; i++) {
for (int j = i + 1; j < students.size(); j++) {
Student s1 = (Student) students.get(i);
Student s2 = (Student) students.get(j);
if (s1.compareTo(s2) > 0) {
students.set(i, s2);
students.set(j, s1);
}
}
}
}
public void save() throws IOException {
PrintWriter out = new PrintWriter(filename);
out.println(title);
for (int i = 0; i < students.size(); i++) {
Student s = (Student) students.get(i);
out.println(s.toString());
}
out.close();
}}
主要方法
public class Assignment4 {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.print("Enter the name of the input file ->");
String fileName = input.next();
ClassRoll c = new ClassRoll(fileName);
prompt();
System.out.print("Enter a command --> ");
String ans = input.next();
while (!(ans.equalsIgnoreCase("q") || ans.equalsIgnoreCase("quit"))) {
if (!(ans.equalsIgnoreCase("a") || ans.equalsIgnoreCase("Add")
|| ans.equalsIgnoreCase("s")|| ans.equalsIgnoreCase("average")
|| ans.equalsIgnoreCase("n")|| ans.equalsIgnoreCase("names")
|| ans.equalsIgnoreCase("r") || ans.equalsIgnoreCase("remove")
|| ans.equalsIgnoreCase("f") || ans.equalsIgnoreCase("find")
|| ans.equalsIgnoreCase("d") || ans.equalsIgnoreCase("display"))) {
System.out.println("Bad Command");
} else {
switch (ans.charAt(0)) {
case 'a':
c.Add();
break;
case 's':
c.sortAverage();
c.Display();
break;
case 'n':
c.sortNames();
c.Display();
break;
case 'r':
c.Remove();
c.Display();
break;
case 'f':
Student s = c.find();
if (s == null) {
System.out.println("Student not found");
} else {
System.out.println(s.toString());
}
break;
case 'd':
c.Display();
break;
}
}
prompt();
System.out.print("Enter a command --> ");
ans = input.next();
}
c.save();
System.out.println("Thank you for using this program");
}
public static void prompt() {
System.out.println("Enter one of the following commands");
System.out.println("a or Add to add a student in the classroll");
System.out.println("sa or average to sort the students based on their average");
System.out.println("sn or names to sort the students based on their last names");
System.out.println("r or remove to remove a student from the class roll");
System.out.println("f or find to find a student in the class roll");
System.out.println("d or display to display the class roll");
System.out.println("q or quit to exit the program");
}
}
学生课程
public class Student {
private String fName;
private String lName;
private Exams scores;
Student(String fn, String ln) {
fName = fn;
lName = ln;
scores = new Exams();
}
public void setScore1(int sc) {
int score1 = 0;
score1 = sc;
}
public void setScore2(int sc) {
int score2 = 0;
score2 = sc;
}
public void setScore3(int sc) {
int score3 = 0;
score3 = sc;
}
public String toSring() {
return String.format("%-10s %-10s %4.2f\n", fName, lName, scores);
}
public double getAverage() {
return (scores.getScore1() + scores.getScore2()
+ scores.getScore3()) / 3.0;
}
public int compareTo(Student s) {
String name1 = lName + " " + fName;
String name2 = s.lName + " " + s.fName;
if((lName + " " + fName).compareTo(s.lName + " " + s.fName)> 0)
return 1;
else if ((lName + " " + fName).compareTo(s.lName + " " + s.fName)< 0)
return -1;
else return 0;
}
}
答案 0 :(得分:0)
扫描程序抛出此异常
由扫描程序抛出,表示检索到的令牌没有 匹配预期类型的模式,或者令牌不在 预期类型的范围。
请检查您的输入文件。它会解决你的问题。
您的数据必须以行分隔。
代码段
Scanner kb = new Scanner(new FileInputStream(new File("d://app//test.txt")));
boolean firstline = true;
while(kb.hasNext()) {
if (firstline) {
title = kb.nextLine();
firstline = false;
} else {
String fName =kb.next() ;
String lName = kb.next();
int score1 = kb.nextInt();
int score2 = kb.nextInt();
int score3 = kb.nextInt();
students.add(new Student(fName, lName));
}
}