我想要做的是读取3个文件,并将它们存储在名为Student的数组中,并打印出使用readIRstudent,readIR101和readIR102读取的文件。我的问题是,当我执行代码时,只有第一个readIRstudent实际打印出来在我的测试类中,我不能让数组工作。这是我的代码。
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by Lewis on 28/03/2015.
*/
public class Task2 {
public Scanner input;
int reg;
double ir101,ir102l;
double aggregate;
String student_name;
public void openIRstud()throws Exception{
JFileChooser IRstudchooser = new JFileChooser();
if(IRstudchooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
java.io.File inFile = IRstudchooser.getSelectedFile();
input = new Scanner(inFile);
System.out.println("IRstudents file read successfully.");
}
}
public void openIR101()throws Exception{
JFileChooser IR101chooser = new JFileChooser();
if(IR101chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
java.io.File inFile = IR101chooser.getSelectedFile();
input = new Scanner(inFile);
System.out.println("IR101 file read successfully.");
}
}
public void openIR102()throws Exception{
JFileChooser IR102chooser = new JFileChooser();
if(IR102chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
java.io.File inFile1 = IR102chooser.getSelectedFile();
input = new Scanner(inFile1);
System.out.println("IR102 file read successfully.");
}
}
public void readIRstud (){
while(input.hasNext()){
String irstudid = input.next();
String irname = input.next();
System.out.printf(" %s %s\n", irstudid, irname);
}
System.out.println("Complete.");
}
public void readIR101 (){
while(input.hasNext()){
String ir101studid = input.next();
String ir101mark = input.next();
System.out.printf(" %s %s\n", ir101studid, ir101mark);
}
System.out.println("Complete.");
}
public void readIR102 (){
while(input.hasNext()){
String ir102studid = input.next();
String ir102mark = input.next();
System.out.printf(" %s %s\n", ir102studid, ir102mark);
}
System.out.println("Complete.");
}
public void Student(){
ArrayList<Student> students = new ArrayList<>();
while (input.hasNext()) {
String line = input.nextLine();
Student e = new Student();
e.reg = Integer.parseInt(line.split(" ")[0]);
e.ir101 = Double.parseDouble(line.split(" ")[1]);
students.add(e);
}
}
}