Question

I have to count the occurrences of each String in an ArrayList that includes Strings and ints. Right now I have to ignore the int variable that corresponds to the quantity of every item and just count the repetitions of each String in that list.

My problem is that in class we only did this with ints. Now with Strings I'm having a problem with the casing because "abc" is different than "Abc" and "def of Ghi" is different from "Def of ghi".

Right now the code I have is this:

Map<String, Integer> getCount1 = new HashMap<>();
{
    for (ItemsList i : list) {
        Integer count = getCount1.get(i.name);
        if (count == null) {
            count = 0;
        }
        getCount1.put(i.name, (count.intValue() + 1));
    }

    for (Map.Entry<String, Integer> entry : getCount1.entrySet())
        f.println(entry.getKey() + " : " + entry.getValue());
}

But as I said: it is not counting the occurrences correctly. I have, for example, one occurrence in my list called "Abc of abc" then in the input.txt file list I have that same occurrence 4 times - "Abc of abc"; "abc of Abc"; "Abc of Abc" and "abc Of abc" - all written differently and it's counting them separately instead of the same occurrence 4 times.

Early on when I was working on the totals and averages I was able to use equalsIgnoreCase() so it works fine in there, so regardless of the casing, it's counting them in the right list but not as just one occurrence several times.

Is there a way that I could use the ignore case or convert everything to the same case before counting them?

Just an update: Instead of trying the .toLowerCase() there I used it in the FileReader when it reads the .txt file and it worked i.name = name.toLowerCase();

Thanks for your time and help anyway

Was it helpful?

Solution 3

Instead of trying the .toLowerCase() there I used it in the FileReader when it reads the .txt file and it worked i.name = name.toLowerCase();

So in the end my code there was this:

static void readFile(ArrayList<Items> list) throws IOException {
    BufferedReader in = new BufferedReader(
        new FileReader("input.txt")
    );
    String text;
    while( (text = in.readLine()) != null ) {
        if(text.length()==0) break;
        Scanner line = new Scanner(text);
        linha.useDelimiter("\\s*:\\s*");
        String name = line.next();
        int qtt = line.nextInt();
        Items i = new Items();
        i.name = name.toLowerCase();
        i.qtt = qtt;
        list.add(i);
    }
    in.close();            
}

OTHER TIPS

Try this :

public void getCount(){
    Map<String, Integer> countMap = new HashMap<String, Integer>();
    for(ItemsList i : itemsList){
        if(countMap.containsKey(i.Name.toLowerCase())){
            countMap.get(i.Name.toLowerCase())++;
        }
        else{
            countMap.put(i.Name.toLowerCase(),1);
        }
    }
}

The hashing function for a HashMap is case sensitive, so you need to uppercase or lowercase the string values. See below your modified code:

Map<String, Integer> getCount1 = new HashMap<>();
{
    for (ItemsList i : list) {
        Integer count = getCount1.get(i.name);
        if (count == null) {
            count = 0;
        }
        getCount1.put(i.name.toString(). toLowerCase() , (count.intValue() + 1));
    }

    for (Map.Entry<String, Integer> entry : getCount1.entrySet())
        f.println(entry.getKey() + " : " + entry.getValue());
}

As a point of style I'd use a more descriptive name for the items in your ItemsList like item.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top