counting the repeated values of a certain row in array

Discussion in 'Java' started by dmonx, Sep 26, 2012.

  1. dmonx

    dmonx

    Joined:
    Sep 26, 2012
    Messages:
    1
    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));
            
    }
           
        }
    }
    
    dmonx, Sep 26, 2012
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Gibbs
    Replies:
    2
    Views:
    466
    Ian Gibbs
    May 23, 2004
  2. arnuld
    Replies:
    10
    Views:
    1,733
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 3, 2007
  3. D
    Replies:
    0
    Views:
    197
  4. Michele Dondi
    Replies:
    0
    Views:
    162
    Michele Dondi
    Nov 15, 2007
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    186
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page