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");
        }
    }
} 
dev2d
  • 4,245
  • 3
  • 31
  • 54
Little bird
  • 1,106
  • 7
  • 28
  • 58

8 Answers8

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. :-)

JNL
  • 4,683
  • 18
  • 29
  • @Littlebird just check if two array contains same elements or not. see http://stackoverflow.com/questions/14897366/comparing-two-array-in-java – Anirban Nag 'tintinmj' Nov 19 '13 at 16:52
  • 1
    Downvoter... Please leave a comment so that it helps to understand the missing point if any. – JNL Nov 19 '13 at 17:28
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++)
    {
Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
  • 1
    code **i<=s.length** and **j<=s1.length** is incorrect. You should use j< s1.length and i < s.length instead. – Mengjun Nov 19 '13 at 16:44
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++)
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
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");
            }
}
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

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
user1339772
  • 783
  • 5
  • 19
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++)
        {
1

Your loops are not correct. Try

   for (int i=0; i<s.length; i++)
    {
        for (int j=0; j<s1.length; j++)
        {
Masudul
  • 21,823
  • 5
  • 43
  • 58
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

dev2d
  • 4,245
  • 3
  • 31
  • 54