0

I'm new to java and just wrote a program for learning purpose. I have two arrays of strings and i want to compare the length of both string arrays.If lengths are equal then compare the values of each first string array with the values of each second array .If value got matched then print that value. Not able to rectify where the problem is ?

     package com.equal.arrat;

    import java.util.ArrayList;
    import java.util.List;

       public class ArrayEqual {
         public static void main(String[] args) {

        String s[] = {"anuj","kr","chaurasia"};
        String s1[] = {"anuj","kr","chaurasia"};

        if (s.length==s1.length)
        {
            System.out.println(s.length);
            for (int i =0 ; i>=s.length;i++)
            {
                for (int j =0 ;j>=s1.length;j++)
                {
                System.out.println("test");
                if (s[i].equals(s1[j]))
                {
                System.out.println("ok" + s[i]);
                }
                else{
                    System.out.println("not ok");
                    }
        }
        }
       }
        else{
            System.out.println("Length Not Equal");
        }
    }
} 

8 Answers 8

5

Incorrect logic used. What you meant was < instead of >=

for (int i =0 ; i>=s.length;i++) 

should be

for (int i =0 ; i<s.length;i++)

Hope this helps. :-)

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

2 Comments

@Littlebird just check if two array contains same elements or not. see stackoverflow.com/questions/14897366/…
Downvoter... Please leave a comment so that it helps to understand the missing point if any.
3

At first you are saying i and j is 0 then checking in for loop that if it is greater than s.length or not which is false so it's not executing. Try

for (int i =0 ; i<s.length;i++)
{
    for (int j =0 ;j<s1.length;j++)
    {

1 Comment

code i<=s.length and j<=s1.length is incorrect. You should use j< s1.length and i < s.length instead.
2

You have errors in your for loop condition checks. This line -

for (int i = 0; i >= s.length; i++)

should be changed to this -

for (int i = 0; i < s.length; i++)

Similarly, change this line -

for (int j = 0; j >= s1.length; j++)

to this -

for (int j = 0; j < s1.length; j++)

3 Comments

Using <= means that it will also execute with i == s.length which is out of bounds.
@FatalError: Sorry, forgot to remove the = part. Thank you for pointing that out.
@Downvoter: A comment is usually helpful to correct the mistakes.
2

your comparators are off in the for loops. That may be why its not working

if (s.length==s1.length)
{
    System.out.println(s.length);
    for (int i =0 ; i<s.length;i++)
    {
        for (int j =0 ;j<s1.length;j++)
        {
        System.out.println("test");
        if (s[i].equals(s1[j]))
        {
        System.out.println("ok" + s[i]);
        }
        else{
            System.out.println("not ok");
            }
}

Comments

2
for (int i =0 ; i>=s.length;i++) 

On first iteration i=0 and s.length=3 and i>=s.length would translate to 0>=3 which is false and hence the loop will not execute. It should be i<=s.length

Comments

2

You want to compare the length of both string arrays.If lengths are equal then compare the values of each first string array with the values of each second array .so you cant use this >= for comparing length.

simply try it

 for (int i =0 ; i<s.length;i++)
    {
        for (int j =0 ;j<s1.length;j++)
        {

Comments

1

change for (int i =0 ; i>=s.length;i++) to

for (int i =0 ; i<s.length;i++)

and same for inner loop

because your first condition itself is not getting satisfied i=0 and you are checking if i>=s.length(means 3 in this case)

so your for loop will not be executed

Comments

1

Your loops are not correct. Try

   for (int i=0; i<s.length; i++)
    {
        for (int j=0; j<s1.length; j++)
        {

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.