在下面的代碼中,我有一種方法,它應該從文本文件(姓,名,類名)中獲取數據並告訴學生是否在場(出席),然後用「僅」學生的值填充表格的次數(基本上小於輸入到文本字段中指定的次數)。我嘗試使用散列表,但我不確定要放置「放置」語句以便正確填充散列表。我在表中重複了一些信息,我不想重複。我的代碼如下:任何幫助將不勝感激。使用HashMap將數據輸入表
public void processFile() throws FileNotFoundException{
DefaultTableModel model = (DefaultTableModel) this.jTable_areasOfConcern.getModel();
File g = new File("pupilSortTemp.txt");
InputStream is;
Scanner scan = null;
HashMap<Integer, String> attendanceList = new HashMap<>();
try {
String firstName;
String lastName;
String className;
String studentKey;
String tab = "\t";
String attendance;
int attendanceCount = 0;
int totalDaysOrLessStudentsPresent;
totalDaysOrLessStudentsPresent = Integer.valueOf(this.jTextField_totalDays.getText());
is = new FileInputStream(g);
scan = new Scanner(is);
String[] array;
String line = scan.nextLine();
if (line.contains(tab)) {
array = line.split(tab);
}
else {
array = line.split("\n");
}
firstName = array[0];
lastName = array[1];
className = array[2];
attendance = array[4];
System.out.println("firstName=" + firstName);
System.out.println("lastName=" + lastName);
System.out.println("className=" + className);
System.out.println("attendance=" + attendance);
if (attendance.equals("Present")){
attendanceCount++;
studentKey = firstName + tab + lastName + tab + className;
attendanceList.put(attendanceCount, studentKey);
System.out.println("attendanceCountIfPresent=" + attendanceCount);
}
System.out.println("attendanceCountIfNotPresent=" + attendanceCount);
while (scan.hasNextLine()) {
line = scan.nextLine();
if (line.contains(tab)) {
array = line.split(tab);
}
else {
array = line.split("\n");
}
System.out.println("array0=" + array[0]);
System.out.println("array1=" + array[1]);
System.out.println("array2=" + array[2]);
System.out.println("array4=" + array[4]);
if (array[0].equals(firstName) && array[1].equals(lastName)){
if (array[4].equals("Present") && (attendanceCount < totalDaysOrLessStudentsPresent)){
attendanceCount++;
//studentKey = firstName + tab + lastName + tab + className;
//attendanceList.put(attendanceCount, studentKey);
System.out.println("attendanceCountIfPresent==" + attendanceCount);
model.addRow(new Object[]{array[2], array[1], array[0], attendanceCount, true});
}
}else {
if (array[4].equals("Present") && (attendanceCount < totalDaysOrLessStudentsPresent)){
attendanceCount = 1;
System.out.println("attendanceCountIfPresent++=" + attendanceCount);
firstName = array[0];
lastName = array[1];
className = array[2];
attendance = array[4];
model.addRow(new Object[]{array[2], array[1], array[0], attendanceCount, true});
studentKey = firstName + tab + lastName + tab + className;
attendanceList.put(attendanceCount, studentKey);
}
else {
attendanceCount = 0;
}
}
//attendanceList.put(attendanceCount, studentKey);
}//end while
for (Map.Entry<Integer, String> entry : attendanceList.entrySet()) {
studentKey = entry.getValue();
attendanceCount = entry.getKey();
array = studentKey.split(tab);
model.addRow(new Object[]{array[2], array[1], array[0], attendanceCount, true});
}
}catch (FileNotFoundException e){
}
finally{
if(scan != null){
scan.close();
}
}
}
感謝您的反饋。我很感激。 – Dante
@丹特請參閱編輯。 –
一旦你的'Map'構建完成,請按照[示例](http://stackoverflow.com/a/9134371/230513)顯示它。 – trashgod