• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

java loop issue

I have 2 strings with string tokens.
f1string and f2string.

I want to write the common string tokens into console.

1)

My output is printing only the Strings which are common s1 and s2
but the counter value that is printing is very big.

Can you please let me know what am I missing.


int counter=0;
                 
  for(int j=0; j<f1string.size(); j++){
        String s1 = (String) f1string.get(j);
              for(int j1=0; j1<f2string.size(); j1++){
                    String s = (String) f2string.get(j1);
                    if (s1.equals(s))
                    {
                    System.out.println("s1 is "+s1);
                    System.out.println("s2 is "+s);}
                           System.out.println(counter);
                    counter++;
                    }
              }                      

0
vkchaitu82
Asked:
vkchaitu82
  • 6
  • 2
1 Solution
 
for_yanCommented:
Do you want tocomparae each token wiuth each token in all combinations - that's what you are doing) or you want to compare first with firts, second with second
0
 
for_yanCommented:
If you want only comparison with corresponding indexces then you don't want embedded loops
Just one loop would be enough
0
 
vkchaitu82Author Commented:
yan, I want to check each token in list1  with all the token in the list2.
its not first to first and second to second.

2) I have counter incrementing only when there is a common string. But it looks it is incrementing more number of times
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
You have one extra brace
0
 
for_yanCommented:
Look after printout of the second line
0
 
for_yanCommented:
As a result you print only eqwuals but incremerent all of them
0
 
vkchaitu82Author Commented:
Thanks Yan . my mistake . I corrected Brace.
(I started using eclipse from yesterday..still not used to its view :(
0
 
for_yanCommented:
It is not extra but it is miplaced shoiuld be aftre the icrementing
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now