[英]putting a text file into an array using jfilechooser for java
我繼續使數組越界,因為該學生可能有不同的分數等級,因此如果一個學生沒有3年級而另一個學生有3年級,我的程序將自動崩潰。 根據學生的不同,我該怎么做才能使程序具有不同的分數等級。
public class StudentList extends Component{
static ArrayList<Student> studentList = new ArrayList<>();
public static final int ARRAYMAX=4;
public void readStudent()throws Exception{
File window = new File(System.getProperty("user.home"));
JFileChooser choice = new JFileChooser();
choice.setCurrentDirectory(window);
int option = choice.showOpenDialog(this);
File selectedFile = choice.getSelectedFile();
if (option == JFileChooser.APPROVE_OPTION) {
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
}
File studentFile = new File(selectedFile.getAbsolutePath());
Scanner in = new Scanner(studentFile);
while (in.hasNext()) {
String data = in.nextLine();
String[] studentData = new String[ARRAYMAX];
studentData = data.split("\\|");
for(int i =0; i<ARRAYMAX; i++){
studentData[i] ="0";
}
String firstName = studentData[0];
String lastName = studentData[1];
double grade1 = Double.parseDouble(studentData[2]);
double grade2 = Double.parseDouble(studentData[3]);
double grade3 = Double.parseDouble(studentData[4]);
Student newStudent = new Student(firstName,lastName);
newStudent.setGrades1(grade1);
newStudent.setGrades2(grade2);
newStudent.setGrades3(grade3);
}
}
}
默認等級(等級1,等級2等)為合理值。 在訪問並設置成績之前,請檢查studentData( studentData.length
)的長度。
另一方面,為什么要使用單個grade
域/設置者? 只需將所有成績存儲在“列表”或“數組”字段中,然后根據需要使用即可。
好吧,如果我正確理解你的話,有些台詞沒有三年級。 如果是這樣,您可以檢查它是否具有三年級,是否具有public class StudentList extends Component{ static ArrayList<Student> studentList = new ArrayList<>(); public static final int ARRAYMAX=4; public void readStudent()throws Exception{ File window = new File(System.getProperty("user.home")); JFileChooser choice = new JFileChooser(); choice.setCurrentDirectory(window); int option = choice.showOpenDialog(this); File selectedFile = choice.getSelectedFile(); if (option == JFileChooser.APPROVE_OPTION) { System.out.println("Selected file: " + selectedFile.getAbsolutePath()); } File studentFile = new File(selectedFile.getAbsolutePath()); Scanner in = new Scanner(studentFile); while (in.hasNext()) { String data = in.nextLine(); String[] studentData = new String[ARRAYMAX]; studentData = data.split("\\\\|"); for(int i =0; i<ARRAYMAX; i++){ studentData[i] ="0"; } String firstName = studentData[0]; String lastName = studentData[1]; double grade1 = Double.parseDouble(studentData[2]==null?"0":studentData[2]); double grade2 = Double.parseDouble(studentData[3]==null?"0":studentData[3]); double grade3 = Double.parseDouble(studentData[4]==null?"0":studentData[4] ); Student newStudent = new Student(firstName,lastName); newStudent.setGrades1(grade1); newStudent.setGrades2(grade2); newStudent.setGrades3(grade3); } }
public class StudentList extends Component{ static ArrayList<Student> studentList = new ArrayList<>(); public static final int ARRAYMAX=4; public void readStudent()throws Exception{ File window = new File(System.getProperty("user.home")); JFileChooser choice = new JFileChooser(); choice.setCurrentDirectory(window); int option = choice.showOpenDialog(this); File selectedFile = choice.getSelectedFile(); if (option == JFileChooser.APPROVE_OPTION) { System.out.println("Selected file: " + selectedFile.getAbsolutePath()); } File studentFile = new File(selectedFile.getAbsolutePath()); Scanner in = new Scanner(studentFile); while (in.hasNext()) { String data = in.nextLine(); String[] studentData = new String[ARRAYMAX]; studentData = data.split("\\\\|"); for(int i =0; i<ARRAYMAX; i++){ studentData[i] ="0"; } String firstName = studentData[0]; String lastName = studentData[1]; double grade1 = Double.parseDouble(studentData[2]==null?"0":studentData[2]); double grade2 = Double.parseDouble(studentData[3]==null?"0":studentData[3]); double grade3 = Double.parseDouble(studentData[4]==null?"0":studentData[4] ); Student newStudent = new Student(firstName,lastName); newStudent.setGrades1(grade1); newStudent.setGrades2(grade2); newStudent.setGrades3(grade3); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.