Count Method Java

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

}

}

4

2 Answers

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

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.");
}
6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest