Results 1 to 2 of 2
  1. #1
    dalonehunter is offline Member
    Join Date
    Oct 2009
    Rep Power

    Default Need help with counting strings in an array.

    Hey guys, as you'll see I'm kind of new to Java so I don't know too much and I came across this site looking for help. I'm making a program for my java class and the first part of the assignment is to make it take grades from a teacher and then count the number of each grade. For example, it would count 4 A's, 3 B's, 1 C, etc... The problem I have is counting the number of each letter grade in the array. This is what I have so far:
    Java Code:
    import javax.swing.JOptionPane;
    public class Grading {
    	public static void main(String[] args) {
    		int a;
    		a=Integer.parseInt(JOptionPane.showInputDialog("How many students are in your class?"));
    		String grades[]=new String[a];
    		for(int i=0;i<grades.length;i++)
    		{grades[i]=JOptionPane.showInputDialog("What are their grades?");
    So I have the person enter the number of students and than enter that many grades into an array and that works fine. The problem is counting the number of each grade. I tried using some If, else statements but to no avail. Can you guys help a noobie out? Thanks.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Rep Power


    You can count letter frequencies using a Map<String, Integer>.
    You loop through the array, if the letter is in the array (Integer count = Map.get(letter)) increment it's count and set that in the map Map.put(letter, newCount). Otherwise add the letter to the map with a count of 1.

Similar Threads

  1. Array help counting # of grades
    By speaknspell in forum New To Java
    Replies: 4
    Last Post: 04-16-2009, 11:09 PM
  2. Replies: 3
    Last Post: 12-14-2008, 02:52 PM
  3. Counting Duplicate Variables in an Array
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 08:33 PM
  4. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 06:27 AM
  5. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts