0
String[] testing = new String[]{"Hat", "Cat", "Bear", "Tiger", "Bird"};

Using the array above and generating it in a random order, I'm trying to check each String to the other, and if any are the same, then checking goes up by 1.

Ex: if testing[0] == testing[1], checking++, if not, test[0] == test[2] and so on.

However, I'm just not getting my logic right as its giving me random integers that I'm not expecting. If anyone can point out my errors id appreciate it.

Note: I'm new to Java and do not know a lot of methods out there.

    public int checkUp() {
        int checking = 0;
        int looper = 0;
        int checker = 0;
        for (int i = 1; i < testing.length; i++) {
            while (checker < testing.length) {
                if (String.valueOf(testing[looper]).equals(String.valueOf(testing[i]))) {  
                    checker++;
                } else {
                    looper++;
                }
                checker++;
            }
        }
        return checking;
    }
5
  • 5
    You return checking; but you never do anything with that variable beside initializing it with int checking = 0;. So I'm surprised you say you are getting "random integers" when all you should ever get returned from that method is a very non-random 0 Commented Feb 26, 2021 at 14:19
  • I would use two for-loops inside another: for (int i...) { for (int j...) { if (testing[i] is equal to testing[j]) { checking++; } } } Commented Feb 26, 2021 at 14:25
  • return testing.length - (int) Stream.of(testing).distinct().count(); Commented Feb 26, 2021 at 14:25
  • 1
    If 3 strings are the same, by how much should the counter be incremented? Is it 2 (because string1 == string2 and string1 == string 3) or is it 3 (string1==string2, string1==string3, string2=string3)? Commented Feb 26, 2021 at 16:45
  • @k314159 So all strings dont match then 0, if string1 == string 2 or string1== string etc then its 1. if string 1 == string 2 && string 1== string 3 then 2. if all strings match then output is 4. I hope this helps b/c im still having a hard time understanding Commented Feb 27, 2021 at 1:11

2 Answers 2

1
public int checkUp() {
    int looper = 0;
    int checking = 0;
    while (looper < testing.length) {
        int checker = looper + 1;
        while (checker < testing.length) {
            if (testing[looper].equals(testing[checker])) {  
                checking++;    
            }
            checker++;
        }
    }
    return checking;
}

This would return the total number of matching strings. Note : you need not use String.valueOf since the elements in the array are already of type String

Sign up to request clarification or add additional context in comments.

Comments

1

There's a simpler way to do this using a Set: Add all elements of your array into a set. Your result is then the difference between the array length and set size.

Something like this:

String[] input = new String[] {"Hat", "Cat", "Bear", "Tiger", "Bird"};
Set<String> set = new HashSet<String>(Arrays.toList(input));
int result = input.length - set.size();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.