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