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++;
                    }
              }                      

vkchaitu82Asked:
Who is Participating?
 
for_yanCommented:
You have one extra brace
0
 
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.