简体   繁体   中英

Count Method Java printing correctly

So I have a program that opens a file "input.txt" which contains a list of names. I have multiple methods in order to organize the names, capitalize accordingly, and display them. What I am having trouble on is in the output aspect of the program. When I use the countOccurrence method which I will have shown below, it correctly counts how many times each name appears in the list whether it is a first or last name. The problem is it not displaying the count the way I want it to. I will have the output I get and the output I want shown below and if anyone can help that would greatly appreciated.

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

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("\n*****First Name Count***");

    sort(first);

    for(int i =0; i < first.size(); i++){
            int count = countOccurence(first.get(i), first);
            System.out.println(first.get(i) + "        " + count + " times.");
        }


    System.out.println("\n****Last Name Count****");

    sort(last);

    for(int i =0; i < last.size(); i++){
            int count = countOccurence(last.get(i), last);
            System.out.println(last.get(i) + "        " + count + " times.");
        }


    System.out.println("\n****All Names Count****");

    sort(allNames);

    for(int i =0; i < last.size(); i++){
            int count = countOccurence(allNames.get(i), allNames);
            System.out.println(allNames.get(i) + "        " + count);
        }



    *****First Name Count***
    Adriana        4 times.
    Adriana        4 times.
    Adriana        4 times.
    Adriana        4 times.
    Colette        4 times.
    Colette        4 times.
    Colette        4 times.
    Colette        4 times.
    Emmanuel       1 times.
    Gerri          1 times.
    Gretta         1 times.
    Kirsten        2 times.
    Kirsten        2 times.
    Marcia         2 times.
    Marcia         2 times.
    Neva           1 times.
    Shawanda        1 times.
    Tijuana        1 times.

    ****Last Name Count****
    Beres        2 times.
    Beres        2 times.
    Beumer       1 times.
    Hutt         2 times.
    Hutt         2 times.
    Jones        2 times.
    Jones        2 times.
    Koenig       1 times.
    Means        1 times.
    Montilla     4 times.
    Montilla     4 times.
    Montilla     4 times.
    Montilla     4 times.
    Mossman      1 times.
    Sapienza     2 times.
    Sapienza     2 times.
    Shover       1 times.
    Stanfill     1 times.

    ****All Names Count****
    Beres, Kirsten        2
    Beres, Kirsten        2
    Beumer, Gretta        1
    Hutt, Colette         1
    Hutt, Shawanda        1
    Jones, Colette        1
    Jones, Marcia         1
    Koenig, Gerri         1
   Means, Tijuana         1
   Montilla, Adriana      4
   Montilla, Adriana      4
   Montilla, Adriana      4
   Montilla, Adriana      4
   Mossman, Emmanuel      1
  Sapienza, Colette       2
  Sapienza, Colette       2
  Shover, Neva            1
  Stanfill, Marcia        1

******* First Names count*********

Adriana 4

Colette 4

Emmanuel 1

Gerri 1

Gretta 1

Kirsten 2

Marcia 2

Neva 1

Shawanda 1

Tijuana 1

******* Last Names count *********

Beres 2

Beumer 1

Hutt 2

Jones 2

Koenig 1

Means 1

Montilla 4

Mossman 1

Sapienza 2

Shover 1

Stanfill 1

******* All Names count*********

Beres, Kirsten 2

Beumer, Gretta 1

Hutt, Colette 1

Hutt, Shawanda 1

Jones, Colette 1

Jones, Marcia 1

Koenig, Gerri 1

Means, Tijuana 1

Montilla, Adriana 4

Mossman, Emmanuel 1

Sapienza, Colette 2

Shover, Neva 1

Stanfill, Marcia 1

There are two ways to solve this:

  1. Clone the first and last array lists and use the copies to iterate through. After each processed name remove all instances of this name
  2. Create a seperate temp array list and store the already processed names in there, just print the names which are not already stored.

The second way is much more useful, so here goes an example for that:

ArrayList<String> tmp = new ArrayList<String>();
for(int i =0; i < first.size(); i++){
    if(!tmp.contains(first.get(i))) {
        int count = countOccurence(first.get(i), first);
        System.out.println(first.get(i) + "        " + count + " times.");
        tmp.add(first.get(i));
    }
}

If you're using the tmp variable multiple times, don't forget to clear() it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM