Hello, I have created a jsp webpage that allows a user to login and select 4 options from a check box.
The users details are stored in a text file. I am able to read the text file
so it prints like this
Brian opt1 opt4
Bob opt2 opt3
Dave opt4 opt3
Phil opt1 opt4
but I need to be able to count how many times a certain option was chosen. eg:
Opt1 was chosen 2 times Option 4 was chosen 3 times. However the count is not working
I have tried using a tokenizer to break the string apart. Would be great if anyone could help or point me in the right direction
The users details are stored in a text file. I am able to read the text file
so it prints like this
Brian opt1 opt4
Bob opt2 opt3
Dave opt4 opt3
Phil opt1 opt4
but I need to be able to count how many times a certain option was chosen. eg:
Opt1 was chosen 2 times Option 4 was chosen 3 times. However the count is not working
I have tried using a tokenizer to break the string apart. Would be great if anyone could help or point me in the right direction
Code:
BufferedReader br = null;
ArrayList<String> elective = new ArrayList<String>( );
try {
br = new BufferedReader(new FileReader("G:/data1.txt"));
String line;
while ((line = br.readLine()) != null) {
StringTokenizer token = new StringTokenizer(line, "|");
while (token.hasMoreElements()) {
elective.add(token.nextToken());
// String username = token.nextToken().toString();
// String studentCourse = token.nextToken().toString();
// String selections =token.nextToken().toString();
//
//// StringBuilder sb = new StringBuilder();
//// sb.append("\nUsername : " + username);
//// sb.append("\nCourse : " + studentCourse);
//// sb.append("\nElectives : " + selections);
////
//// sb.append("\n*******************\n");
// System.out.println(token.toString());
}
}
} catch (IOException e) {
System.out.println (e.getMessage());
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
String[] titleArray = new String[] {"Knumber" , "Course", "Elective","Count"};
for (int i = 0; i < titleArray.length; i++) {
System.out.print(titleArray[i]+"\t");
}
System.out.println();
for (int i = 0; i < elective.size(); i++) {
System.out.println (elective.get(i));
}
System.out.println();
Set<String> unique = new HashSet<String>(elective);
for (String key : unique) {
System.out.println(key +"\t "+ Collections.frequency(elective, key));
}
}
}