sigma19
asked on
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++;
}
}
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++;
}
}
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
If you want only comparison with corresponding indexces then you don't want embedded loops
Just one loop would be enough
Just one loop would be enough
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Look after printout of the second line
As a result you print only eqwuals but incremerent all of them
ASKER
Thanks Yan . my mistake . I corrected Brace.
(I started using eclipse from yesterday..still not used to its view :(
(I started using eclipse from yesterday..still not used to its view :(
It is not extra but it is miplaced shoiuld be aftre the icrementing