counting the repeated values of a certain row in array

Joined
Sep 26, 2012
Messages
1
Reaction score
0
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



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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top