以下代码应从文件中读取学生及其成绩列表,将它们相互比较,然后显示平均成绩最高的学生
public class CSDept implements Comparable{
private String studentName;
private double java;
private double dataStructure;
private double algorithms;
private int numStudents;
public CSDept(){
}
public CSDept(String studentName,double java,double dataStructure,double algorithms){
this.studentName=studentName;
this.java=java;
this.dataStructure=dataStructure;
this.algorithms=algorithms;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getJava() {
return java+" ";
}
public void setJava(double java) {
this.java = java;
}
public String getDataStructure() {
return dataStructure+" ";
}
public void setDataStructure(double dataStructure) {
this.dataStructure = dataStructure;
}
public String getAlgorithms() {
return algorithms+" ";
}
public void setAlgorithms(double algorithms) {
this.algorithms = algorithms;
}
public int getNumStudents() {
return numStudents;
}
public double getAvg(){
return (java+algorithms+dataStructure)/3;
}
public int compareTo(Object student) {
if(this.getAvg()>((CSDept)student).getAvg()) return 1;
if (this.getAvg()<((CSDept)student).getAvg()) return -1;
return 0;
}
public String toString(){
return studentName+":\n"+"\t"+"Java: "+java+"\t"+"Data Structure : "+dataStructure+"\t"+"Algorithms: "+algorithms+"\n";
}}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class CSDeptFrame extends JFrame{
private JPanel pnlInput=new JPanel(new GridLayout(4,2));
private JPanel pnlOutput=new JPanel(new BorderLayout());
private JPanel pnlFinal=new JPanel(new GridLayout(1,2));
private TitledBorder brdInput=new TitledBorder("Marks");
private TitledBorder brdOutput=new TitledBorder("First Student");
private JLabel lblName=new JLabel("Student Name");
private JLabel lblJava=new JLabel("Java");
private JLabel lblDataStructure=new JLabel("Data Structure");
private JLabel lblAlgorithm=new JLabel("Algorithm");
static JLabel lblFirst=new JLabel("The First Student is :");
static JTextField txtName=new JTextField(20);
static JTextField txtJava=new JTextField(20);
static JTextField txtDataStructure=new JTextField(20);
static JTextField txtAlgorithm=new JTextField(20);
static JButton btnFirst=new JButton("Who is The First Student?");
public CSDeptFrame(String title){
super(title);
pnlInput.setBorder(brdInput);
pnlInput.add(lblName);
pnlInput.add(txtName);
pnlInput.add(lblJava);
pnlInput.add(txtJava);
pnlInput.add(lblDataStructure);
pnlInput.add(txtDataStructure);
pnlInput.add(lblAlgorithm);
pnlInput.add(txtAlgorithm);
pnlOutput.setBorder(brdOutput);
pnlOutput.add(btnFirst,BorderLayout.NORTH);
pnlOutput.add(lblFirst,BorderLayout.SOUTH);
pnlFinal.add(pnlInput);
pnlFinal.add(pnlOutput);
setLayout(new BorderLayout());
add(pnlFinal);}
public static void main(String[] args){
CSDeptFrame frame=new CSDeptFrame("CS DEPT");
frame.setSize(450,200);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
final ArrayList<CSDept> cS= new ArrayList();
File readFile =new File("read.txt");
final File writeFile=new File("write.txt");
try {
Scanner scan=new Scanner(readFile);
while(scan.hasNext()){
CSDept student=new CSDept();
student.setStudentName(scan.next());
student.setJava(scan.nextDouble());
student.setDataStructure(scan.nextDouble());
student.setAlgorithms(scan.nextDouble());
cS.add(student);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "OPS! File is not found");
}
btnFirst.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CSDept firstStudent=new CSDept();
firstStudent.setStudentName(cS.get(0).getStudentName());
firstStudent.setJava(Double.parseDouble(cS.get(0).getJava()));
firstStudent.setDataStructure(Double.parseDouble(cS.get(0).getDataStructure()));
firstStudent.setAlgorithms(Double.parseDouble(cS.get(0).getAlgorithms()));
for (int i=0;i<cS.size();i++){
if (cS.get(i).compareTo(cS.get(i+1))==-1){
firstStudent=cS.get(i+1);
}
}
txtName.setText(firstStudent.getStudentName());
txtJava.setText(firstStudent.getJava());
txtDataStructure.setText(firstStudent.getDataStructure());
txtAlgorithm.setText(firstStudent.getAlgorithms());
lblFirst.setText("The First Student is: "+ txtName.getText());
PrintWriter out;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(writeFile,true)));
for (CSDept cs: cS){
out.println(cs.toString());
out.print("The first student is " + firstStudent.toString());
}
out.close();
} catch (IOException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "OPS! File in Not Found");
}
}
});
}}
但问题是以下陈述不会向arraylist添加任何对象,它的问题是什么?
try {
Scanner scan=new Scanner(readFile);
while(scan.hasNext()){
CSDept student=new CSDept();
student.setStudentName(scan.next());
student.setJava(scan.nextDouble());
student.setDataStructure(scan.nextDouble());
student.setAlgorithms(scan.nextDouble());
cS.add(student);
}
答案 0 :(得分:2)
问题是你试图从空列表中获取特定索引的元素:
final ArrayList<CSDept> cS= new ArrayList();
// ...
public void actionPerformed(ActionEvent e) {
// ...
firstStudent.setStudentName(cS.get(0).getStudentName()); // ArrayList cS is empty here
// ...
也许你可以在访问元素之前添加一个检查以确保列表不为空:
public void actionPerformed(ActionEvent e) {
if(!cS.isEmpty()) {
// do stuff
}
您的列表为空的原因可能是您正在读取空文件&#34; read.txt&#34;因此,while
循环条件永远不会是true
Scanner scan=new Scanner(readFile); // this text file is probably empty
while(scan.hasNext()){ // which makes this condition always false so loop is never executed
CSDept student=new CSDept();
student.setStudentName(scan.next());
student.setJava(scan.nextDouble());
student.setDataStructure(scan.nextDouble());
student.setAlgorithms(scan.nextDouble());
cS.add(student);
}
此外,您在for
内的actionPerformed()
循环中存在ArrayIndexOutOfBoundsException
风险。您将当前元素与下一个元素进行比较,一旦i
对应于最后一个元素的索引,将在比较语句中使用索引i + 1
处的元素给出一个AIOBE:
for (int i=0;i<cS.size();i++){
if (cS.get(i).compareTo(cS.get(i+1))==-1){ // once i = cS.size() - 1, you will get an AIOBE here
firstStudent=cS.get(i+1);
}
}
您可以通过在i = 1
处开始循环并与i - 1
处的元素进行比较或使用forEach循环来解决此问题,这是实现此目标的一种更简洁的方法,因为它不会涉及索引访问。