Hi,

Can anyone tell me what is the problem with the below code:

String eleSrc=srcEle.toString();
      String srcdata_list[]=eleSrc.split("-");
      String eleDest=destEle.toString();
      String destdata_list[]=eleDest.split("-");
      int found=0;
      for(int i=0;i<srcdata_list.length;i++)
      {
         for(int j=0;j<destdata_list.length;i++)
         {
            if(srcdata_list[i].equals(destdata_list[j]))
            {
               found++;
               System.out.println("matches");
               System.out.println("after matches");
               break;
            }
            else
            {
               System.out.println("in else");
               
            }
         }
         System.out.println("out of first for lopp");
        if(found==0)
        {
           System.out.println("Not found..appending to list");
           missingEle.append(srcdata_list[i]);
        }
            
      }

It prints the following O/P:


matches
after matches
out of first for lopp
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else
in else


Why is it not going out of the first for loop????

Dani AI

Generated

Good catch by — the original infinite/strange loop behavior was caused by incrementing the wrong loop variable. fixed that, but the follow-up bug came from managing the match state (found) in a fragile way (resetting it inside the inner else). A much clearer pattern is to treat "matched" as a boolean that is reset at the start of each outer iteration, set true on the first match, break the inner loop, and only append the source token when the boolean is still false after the inner loop.

A cleaner and faster approach (avoids nested O(n*m) loops) is to load the destination tokens into a Set and test membership:

String[] srcTokens = sourceString.split(Pattern.quote("-"));
String[] destTokens = destString.split(Pattern.quote("-"));

Set<String> destSet = new HashSet<>();
for (String t : destTokens) {
  String tt = t.trim();
  if (!tt.isEmpty()) destSet.add(tt);
}

StringBuilder missing = new StringBuilder();
for (String s : srcTokens) {
  String token = s.trim();
  if (!token.isEmpty() && !destSet.contains(token)) {
    if (missing.length() > 0) missing.append("-");
    missing.append(token);
  }
}

Notes and cautions:

  • Use Pattern.quote (or escape special chars) when the delimiter could be a regex metacharacter.
  • Prefer StringBuilder in single-threaded code; StringBuffer only when thread-safety is required.
  • Use equalsIgnoreCase if comparisons should be case-insensitive, and trim() to avoid false mismatches from whitespace.
  • For small arrays a nested loop with a boolean matched is fine; for larger lists use a Set for O(1) lookups.

Recommended Answers

All 3 Replies

Line 8 increments i, should be j?

Line 8 increments i, should be j?

Thanks James, It's working now but now I/m stuck with another problem. :(

If the contents in the array srcdata_list is not found in destdata_list, then that content should be appended to the string buffer(from line 24).

Why is not?? Anu idea??

Hey I got it !!!
guess it was a rookie mistake.
Posting the correct code.

String eleSrc=srcEle.toString();
      String srcdata_list[]=eleSrc.split("-");
      String eleDest=destEle.toString();
      String destdata_list[]=eleDest.split("-");
      int found=0;
      
      for(int i=0;i<srcdata_list.length;i++)
      {
         done:for(int j=0;j<destdata_list.length;j++)
         {
            if(srcdata_list[i].equals(destdata_list[j]))
            {
               found++;
               System.out.println("matches");
               break done;
            }
            else
            {
               found=0;//this was missing
               System.out.println("in else");
               
            }
         }
         System.out.println("found= "+found);
         System.out.println("out of first for lopp");
        if(found==0)
        {
           System.out.println("Not found..appending to list");
           countMatches(1);
           missingEle.append(srcdata_list[i]);
        }
            
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.