Dante
Dante

Reputation: 19

Using HashMap for Entering Data into Table

in the following code, I have a method which is supposed to take data from a text file (lastname, firstname, classname) and tell whether the student was present or not (attendance), then populate a table with the value of "only" students present a certain number of times (basically present less than the times specified by input into a textfield). I tried using a hashmap, but am unsure as to where to put the "put" statement(s) in order to populate the hashmap correctly. I get repeats of information in the table and I do not want duplicates. My code is as follows: Any help would be greatly appreciated.

   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();
        }
        }
    }

Upvotes: 0

Views: 804

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

I don't think that I'd use a HashMap as you're doing it, and if I did, the attendance count certainly would not be used as the key field for the map. All this will do is guarantee that only one student with that attendance count is entered into the collection. Create a Student class, give it the needed fields, including name, perhaps studentId, and yes, attendanceCount, and create a collection of that, perhaps an ArrayList<Student>. Then if you wanted to sort it, you could use a Comparator that sorted the attendanceCount values, or if you wanted to filter it, you could filter it using the same field's value.

Also, I'd have my Student class override equals and hashCode, and would use invariant fields for these methods, and most definitely not the attendance field. If the Student already exists in the ArrayList, by calling contains(student) on the List, then I'd increment the attendance of that student. Otherwise I'd add a new Student to the List.

If you have to use a HashMap, then you would reverse your key and value fields, i.e., have it as a HashMap<Student, Integer> where the value is the attendance count. Again for this to work Student would need to have its equals and hashCode methods overridden using an invariant field or fields within these methods, such as studentID.

Upvotes: 2

Related Questions