comparison of two string

N

Navneet Mathpal

why equal is not comparing it although data is same ???



for (int i=0;i<blen;i++)
{
System.out.println("Best :"+best);
String bb=best;
for(int j=0;j<flen;j++)
{
String ff=found[j];
System.out.println("Found "+found[j]);


if(bb.equalsIgnoreCase(ff))
{
count=count+1;
System.out.println("hello");
resfound.add(found[j]);
}

}




OUTPUT IS :

Best :17601089
Found 18769218
Found 17601089
Found 7240354
Found 18668662
Found 530777
Found 12538756
Found 15441407
Found 11669171
Found 12062082
Found 12626411
Best : 18668662
Found 18769218
Found 17601089
Found 7240354
Found 18668662
Found 530777
Found 12538756
Found 15441407
Found 11669171
Found 12062082
Found 12626411
Best : 9967149
Found 18769218
Found 17601089
Found 7240354
Found 18668662
Found 530777
Found 12538756
Found 15441407
Found 11669171
Found 12062082
Found 12626411
 
A

Andreas Leitgeb

Navneet Mathpal said:
why equal is not comparing it although data is same ???

According to your output, some of the strings in the found array
have an extra whitespace in front. solution: use String.trim()
 
E

Eric Sosman

According to your output, some of the strings in the found array
have an extra whitespace in front. solution: use String.trim()

A hint for the future: When you're printing string values
for debugging purposes, print something that will make their
endpoints visible even if there's leading or trailing space.
For example, instead of the original

System.out.println("Best :"+best);
System.out.println("Found "+found[j]);

you could try something like

System.out.println("Best: <" + best + ">");
System.out.println("Found <" + found[j] + ">");

so that the output

Best: <17601089>
Found < 17601089>

would reveal the discrepancy more clearly.
 
A

Arne Vajhøj

According to your output, some of the strings in the found array
have an extra whitespace in front. solution: use String.trim()

A hint for the future: When you're printing string values
for debugging purposes, print something that will make their
endpoints visible even if there's leading or trailing space.
For example, instead of the original

System.out.println("Best :"+best);
System.out.println("Found "+found[j]);

you could try something like

System.out.println("Best: <" + best + ">");
System.out.println("Found <" + found[j] + ">");

so that the output

Best: <17601089>
Found < 17601089>

would reveal the discrepancy more clearly.


That is very good advice.

I would use || not <>, but that does not matter.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top