2017-02-12 50 views
0

在下面的代碼中,我有一種方法,它應該從文本文件(姓,名,類名)中獲取數據並告訴學生是否在場(出席),然後用「僅」學生的值填充表格的次數(基本上小於輸入到文本字段中指定的次數)。我嘗試使用散列表,但我不確定要放置「放置」語句以便正確填充散列表。我在表中重複了一些信息,我不想重複。我的代碼如下:任何幫助將不勝感激。使用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(); 
     } 
     } 
    } 

回答

2

我不認爲我會使用HashMap爲你這樣做了,如果我這樣做,出席伯爵肯定會被用來作爲地圖的關鍵領域。所有這些都將保證只有一名具有該出席人數的學生被錄入收藏。創建一個Student類,給它所需的字段,包括名稱,可能是studentId,是,attendanceCount,並創建一個集合,可能是ArrayList<Student>。然後,如果您想對其排序,則可以使用對考勤計數值進行排序的比較器,或者如果您想對其進行過濾,則可以使用相同字段的值對其進行過濾。

此外,我會有我的學生班覆蓋equals和hashCode,並將使用這些方法的不變字段,並最肯定而不是考勤領域。如果學生已經存在於ArrayList中,通過在列表上調用contains(student),那麼我會增加該學生的出勤率。否則,我會添加一個新學生到列表中。

如果您必須使用HashMap,則您需要將您的密鑰和值字段顛倒過來,即將其作爲HashMap<Student, Integer>,其中值爲考勤計數。再次,爲了這個工作,Student需要使用這些方法中的不變字段或字段(例如studentID)重寫equals和hashCode方法。

+0

感謝您的反饋。我很感激。 – Dante

+0

@丹特請參閱編輯。 –

+0

一旦你的'Map'構建完成,請按照[示例](http://stackoverflow.com/a/9134371/230513)顯示它。 – trashgod

相關問題