Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

comparing i th index string with 0 to i-1 th index string using '==' operator

for(int i=0; i<n; i++) {
            array[i] = sc.nextLine();
            int count=0;
            for(int j=0; j<i; j++) {
                if(array[i]==array[j])count++;
            }
}

I am taking n number of inputs. suppose n=5.

5
a
b
c
a
a

here, 3 inputs are similar.
If I print the value of count for every i index the output should be like:

0
0
0
1
2

But actually it’s printing:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

0
0
0
0
0

Full code:

package pac1;
import java.util.Scanner;

public class Simple{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        sc.nextLine();
        
        String[] array = new String[n];
        
        
        for(int i=0; i<n; i++) {
            array[i] = sc.nextLine();
            int count=0;
            for(int j=0; j<i; j++) {
                if(array[i]==array[j])count++;
            }
            System.out.println(count);
        }   
    }   
}

But when I compare same index:

if(array[i]==array[i]) count++;

it’s working, value of count is increased.
I can’t figure out why it’s happening. I am new in Java.

>Solution :

The issue with your code is that you’re using the == operator to compare strings in Java. The == operator checks if two references are pointing to the same object in memory, not if they have the same value. So in your code, the == operator will only return true if two strings are the same object in memory, which is unlikely to happen unless you explicitly create two variables that point to the same string object.

To compare the values of two strings in Java, you should use the .equals() method instead. This method will return true if two strings have the same value, regardless of whether they are the same object in memory or not.

Here’s an example:

String str1 = "Hello";
String str2 = "Hello";

if (str1 == str2) {
    // This code block will NOT be executed because str1 and str2
    // are not the same object in memory, even though they have
    // the same value.
}

if (str1.equals(str2)) {
    // This code block will be executed because str1 and str2
    // have the same value, even though they are not the same
    // object in memory.
}

In your code, you can fix the issue by replacing the == operator with the .equals() method in the if statement:

for (int i = 0; i < n; i++) {
    array[i] = sc.nextLine();
    int count = 0;
    for (int j = 0; j < i; j++) {
        if (array[i].equals(array[j])) {
            // Use .equals() method to compare the values of the strings
            count++;
        }
    }
    System.out.println(count);
}

With this change, your code should produce the output that you expect.

Hope this helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading