user541597
user541597

Reputation: 4345

Comparing string to other strings sequencially

I want to be able to compare one string to all other strings sequentially and then move down to the next string and compare that strings to all the other strings below it.

Here is the code I have. It compares the first string correctly and finds no matches. However when the other for loop moves to the next value of i, everystring =1 and so does i so it compares itself. How can I go about moving the inner for loop one value each time I go through the other loop?

for (int everystring = 0; everystring < children.length; everystring++) {

   String current = children[everystring].substring(0,
         children[everystring].indexOf("_"));

   for (int i = 1; i < children.length; i++) {
      String innercurrent = children[i].substring(0,
            children[i].indexOf("_"));

      if (current.equalsIgnoreCase(innercurrent)) {

         System.out.println("Match");
      } else
         System.out.println("No Match");
   }
   System.out.println();
}

Upvotes: 0

Views: 154

Answers (5)

user1181445
user1181445

Reputation:

This.
for (int everystring = 0; everystring < children.length - 1; everystring++) {

   String current = children[everystring].substring(0,
         children[everystring].indexOf("_"));

   for (int i = everystring + 1; i < children.length; i++) {
      String innercurrent = children[i].substring(0,
            children[i].indexOf("_"));

      if (current.equalsIgnoreCase(innercurrent)) {

         System.out.println("Match");
      } else
         System.out.println("No Match");
   }
   System.out.println();
}

Upvotes: 0

Caesar Ralf
Caesar Ralf

Reputation: 2233

That's a strange code you got there .__. but I would do something like this:

for (int i = 0; i < children.length - 1; i++) {
   String current = children[i].substring(0,children[i].indexOf("_"));

   for (int j = i + 1; j < children.length; j++) {
       String compareTo = children[j].substring(0,children[j].indexOf("_"));

       if (current.equalsIgnoreCase(compareTo))
           System.out.format("%s matches %s\n", current, compareTo);
       else
           System.out.format("%s doesn't matches %s\n", current, compareTo);
   }
}

this way you compare everything only once and you don't compare the same position to itself.

Upvotes: 0

user541597
user541597

Reputation: 4345

 int i = 0;      

   for (int everystring = 0; everystring<children.length; everystring++){

       String current = children[everystring].substring(0,children[everystring].indexOf("_"));

       i = everystring+1;

   for (;i<children.length; i++){
       String innercurrent = children[i].substring(0,children[i].indexOf("_"));



      if(current.equalsIgnoreCase(innercurrent)){

      System.out.println("Match");
      }else System.out.println("No Match");

      /* if (current.substring(0,current.indexOf("_")).equalsIgnoreCase(innercurrent.substring(0,innercurrent.indexOf("_")))){




           System.out.println("Match");


       }*/

   }
   System.out.println();
   }
}

Upvotes: 0

Chris
Chris

Reputation: 23171

If I understand your question correctly, all you need to do is use the value of everystring in the initializer of your inner loop:

for (int everystring = 0; everystring < children.length; everystring++) {
    String current = children[everystring].substring(0,
     children[everystring].indexOf("_"));
    for (int i = everystring+1; i < children.length; i++) {
        String innercurrent = children[i].substring(0,
        children[i].indexOf("_"));

        if (current.equalsIgnoreCase(innercurrent)) {

            System.out.println("Match");
        } else
           System.out.println("No Match");
        }
    System.out.println();
 }

This will compare each string to all strings that appear after it in the array.

Upvotes: 1

amit
amit

Reputation: 178431

loop always from 0 to children.length [in inner loop], and add the following condition in the beginning of the inner loop:

if (i == everything) continue;

It will skip every iteration of the inner loop where i == everything, so you will only check for strings that are unequal.

Note however that you will check each 2 strings twice (for example: you will check i == 1, everything == 2 and i == 2, everything == 1
If you don't want it: iterate in inner loop from everything + 1 until children.length

Upvotes: 1

Related Questions