0

I have this code here and it makes sense to me that it would print out how many instances occur in the array of the element at array[i]. The count never goes above one, what am I doing wrong?

import java.util.Scanner;
public class poker
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);
        String[] array = new String[5];
        for(int i = 0; i < array.length; i++)
        {
            array[i] = scan.nextLine();
        }
        for (int i = 0; i < array.length; i++) {
            int count = 0;
            for (int j = 0; j < array.length; j++) {
                {
                    if (array[i] == array[j]) {
                        count++;
                    }

                }
                if (count >= 3) {
                    System.out.println(array[i] + " exists " + count + " times.");
                }
            }
        }
    }
}
javabeginner
  • 83
  • 1
  • 10

4 Answers4

2

Your comparison of the datatype String won't work unless it's the same instance. To just check if the strings are having the same characters you should use .equals(). Keep in mind that the comparison of Integer (not int) also works like this. The reason behind that is that String is a class and not a primitve type. Also read up on this post.

So with

if (array[i].equals(array[j])) {
    count++;
}

you should be fine.


Additional (more readable) solution

To give you an advanced approach on how to count same values i created a small sample for you. The function you need is groupingBy. You group all values of a list in one single attribute while counting the occurences.

Example

Map<String, Long> nameAndCount = Arrays.asList(array).stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

for(Entry<String, Long> entry : nameAndCount.entrySet()) {
    System.out.println(entry.getKey() +  " exists " + entry.getValue()+ " times.");
}
jdickel
  • 1,437
  • 1
  • 11
  • 21
  • yeah, that was my mistake I forgot to change the operator. when I change it though it gives me a count of 0 instead of 1 – javabeginner Mar 26 '20 at 14:05
  • 1
    what do you mean by count of 0 or 1? It can't technically give you a count of 0 or 1 since you only output it when the count islarger or equal 3?! – jdickel Mar 26 '20 at 14:07
  • aha I had the less than operator switched around in BlueJ from experimenting, you are totally right. – javabeginner Mar 26 '20 at 14:15
1

this

if (array[i] == array[j])

will only be true when the reference are the same, if you want to compare the string value use

if (array[i].equals(array[j]))
Jocke
  • 2,189
  • 1
  • 16
  • 24
1

To check for equality of strings in java you must use equals() function.
Change the line to if (array[i].equals(array[j])) and you are good to go!
The operator == will return true only when the reference address of the two array elements are same, so that's the reason you never get a count of more than one, as each reference address is an unique one.

0
package com.report.automation;

import java.util.HashMap;
import java.util.Map;

public class Frequency {
    public static void main(String[] args) {
        String value[] = { "Mukesh", "Mukesh", "Sasi", "Senthil", "Mukesh", "Mukesh" };
        String match = "Mukesh";
        int count = 0;
        for (int j = 0; j <= 5; j++) {
            if (match.equals(value[j])) {
                count++;
            }
        }
        System.out.println(count);
    }
}