Count Method Java

37,676

Solution 1

Use Map structure for those case:

Map<String, Integer> recurence = new HashMap<>();
int count;
for (String name : names) {
    if (recurence.containsKey(name)) {
        count = recurence.get(name) + 1;
    } else {
        count = 1;
    }
    recurence.put(name, count);
}

Solution 2

create a method that counts the occurences:

public static int countOccurence(String name, ArrayList<String> names){
     int count = 0;
     for(int i =0; i <= names.size(); i++){
        if(name.equalsIgnoreCase(names.get(i))){
            count++;
        }
     }
     return count;
}

To use it, go through the loop in you Main ( or you can create another method)

for(int i =0; i <= first.size; i++){
    int count = countOccurence(first.get(i), first);
    System.out.println(first.get(i) + " occured " + count + " times.");
}
Share:
37,676
Admin
Author by

Admin

Updated on November 18, 2020

Comments

  • Admin
    Admin over 3 years

    I have an input file called input.txt with a list of names. I have no problem displaying all the names and putting them in alphabetical order with both display and sort methods. But what I am currently struggling to do is create a method where I can count the recurrence of each name in the file. I would grealty appreciate if anyone could help me with this, and find a way to create this method.

    public class Names {
    
    public static void display(ArrayList<String> names) {
        for (int i = 0; i < names.size(); i = i + 1) {
            System.out.println(names.get(i));
        }
    }
    
    public static int find(String s, ArrayList<String> a) {
        for (int i = 0; i < a.size(); i = i + 1) {
            String str = a.get(i);
            if (str.equals(s)) {
                return i;
            }
        }
        return -1;
    
    }
    
    public static void capitalize(ArrayList<String> names) {
        for (int i = 0; i < names.size(); i = i + 1) {
            String name = names.get(i);
            if (!name.isEmpty()) {
                String firstLetter = "" + name.charAt(0);
                names.set(i, firstLetter.toUpperCase() + name.substring(1).toLowerCase());
    
            }
        }
    }
    
    public static void sort(ArrayList<String> names) {
        for (int i = 0; i < names.size() - 1; i = i + 1) {
            int Min = i;
            for (int j = i + 1; j < names.size(); j = j + 1) {
                if (names.get(j).compareTo(names.get(Min)) < 0) {
                    Min = j;
                }
            }
            String tmp = names.get(i);
            names.set(i, names.get(Min));
            names.set(Min, tmp);
    
        }
    
    }
    
    public static void getNames(ArrayList<String> fn, ArrayList<String> ln) throws IOException {
        Scanner kb = new Scanner(System.in);
        System.out.println("What is the input flie?");
        String names = kb.next();
        File inpFile = new File(names);
        Scanner in = new Scanner(inpFile);
    
        while (in.hasNext()) {
            String firstName = in.next();
            String lastName = in.next();
            fn.add(firstName);
            ln.add(lastName);
    
        }
    
    }
    
    private int countOccurence(String name, ArrayList<String> names){
     int count = 0;
     for(int i =0; i <= names.size; i++){
        if(name.equalsIgnoreCase(names.get(i))){
            count++;
        }
     }
     return count;
    

    }

    public static void main(String[] args) throws IOException {
    
        ArrayList<String> first = new ArrayList<>();
        ArrayList<String> last = new ArrayList<>();
        getNames(first, last);
        capitalize(first);
        capitalize(last);
    
        ArrayList<String> allNames = new ArrayList<>();
        for (int i = 0; i < first.size(); i++) {
            allNames.add(last.get(i) + ", " + first.get(i));
        }
        System.out.println("*******All Names******");
    
        sort(allNames);
        display(allNames);
    
        System.out.println("*****First Name Count***");
    
        for(int i =0; i <= first.size; i++){
        int count = countOccurence(first.get(i), first);
    System.out.println(first.get(i) + " occured " + count + " times.");
    

    }

        System.out.println("****Last Name Count****");
    
        sort(last);
        display(last);
    
    }
    

    }

    • D. Ben Knoble
      D. Ben Knoble over 8 years
      Do you need a separate count for each name, or will you take input on the name to count? They suggest different solution..
    • Admin
      Admin over 8 years
      I posted all the methods I have and the main method I have so far
    • Admin
      Admin over 8 years
      I know i have to use the find method or at least I should. But i am confused as to how to change it to count because right now it just gives me the index of where each name first appears in the list
    • jmcg
      jmcg over 8 years
      I think it would be best to create a method private int countOccurence(String name) which takes a first name or last name as an argument and returns the number of occurences. go through the loop and count them.
  • Admin
    Admin over 8 years
    So with this, would i just use it in the main for every name or as a method that I call everytime in the main? Sorry if this is a stupid question, but i am still a beginning programmer.
  • codeaholicguy
    codeaholicguy over 8 years
    yes, with this you can build a data structure like key is the name and value is the recurrence times, so you can get recurrence time by each name on demand
  • Admin
    Admin over 8 years
    I created the method and tried to put what you have for me in the main method, but it keeps giving me an error in the main method saying that i have to create a method countOccurence even though i already have one. Do you know why I may be having this problem?
  • Admin
    Admin over 8 years
    From the method you gave me I have to create a string and then the array list based on the parameters, but what would i put in the string I create?
  • jmcg
    jmcg over 8 years
    sorry, I forgot to include the Arraylist to the method call. countOccurence(first.get(i), first); edited.
  • jmcg
    jmcg over 8 years
    On a side note, you should rename your Arraylist into something that tells you that it's a list e.g. firstNames tells you that it's a list of first names.
  • Admin
    Admin over 8 years
    I updated the code using what you have given but the program won't run. In the method it gives an error in the for loop saying " size has private access in ArrayList". Then in the main method at the function call it gives another error saying " non static method cannot be referenced from a static context". Do you know what I could to do to get it running?
  • Viktor Mellgren
    Viktor Mellgren over 8 years
    Much better solution, since you only need to iterate over the name once, and inserts are fast. O(n) for iteration, and O(1) for put/get
  • jmcg
    jmcg over 8 years
    forgive me for the minor edits. use for(int i =0; i <= first.size(); i++){