Flozza
Flozza

Reputation: 131

Map entry key in java

I'm trying to debug a problem I've on a script, I'm newly to Java I think it's a simplest thing but I need to understand. This :

for( Map.Entry<String,int[]> entry : this.indexMap.entrySet())
{
     if( !entry.getKey().equals("nickname"))
     {
         System.out.print("'"+ entry.getKey() +"' contains "+ entry.getKey().length() +" chars");
         System.out.print("'"+ name +"' contains "+ name.length() +" chars");
     }
     else if( entry.getKey().trim().equals("nickname") )
     {
         System.out.print("Yes are sames");
     } 
}

For a String name = "nickname", displays me that :

18:56:15 [INFOS] 'nickname' contains 94 chars

18:56:15 [INFOS] 'nickname' contains 8 chars

I'm trying to understand this.

The problem is entry.getKey() returns the same thing as my string name, but not really the same. In first test, we saw the two vars are different, so the print is did, but the twos vars have the same value, and not the same length. In the else-if, I tried to remove spaces but not printed so where are from these 94 chars?

https://code.google.com/p/imdbparsers/source/browse/trunk/imdb+parsers/src/imdb/parsers/xmltosql/NamedParameterStatement.java?r=6

Is the code, methods concerned are

private String parse(String query) 

private int[] getIndexes(String name)

line 161 et 89 This for loop i've in mine is only to debug the

 int[] indexes = (int[]) indexMap.get(name);

Returns always null

The query string is :

SELECT COUNT(`account_id`) AS `total` FROM `game_accounts` WHERE `nickname`=:nickname

Upvotes: 0

Views: 1026

Answers (2)

BrianLN
BrianLN

Reputation: 76

I think that if you reverse your if clauses you might get something that behaves more like what you are expecting, although it is somewhat unclear what you are asking. Comparing keys as the first clause in the if block makes the code simpler.

if( entry.getKey().trim().equals("nickname") ) 
{
     System.out.print("Yes are sames");
}
else
{
     System.out.print("'"+ entry.getKey() +"' contains "+ entry.getKey().length() +" chars");
     System.out.print("'"+ name +"' contains "+ name.length() +" chars");
}

Upvotes: 0

Aubin
Aubin

Reputation: 14863

The difference between

entry.getKey().equals("nickname")

and

entry.getKey().trim().equals("nickname")

is trim().

The first take in account the spaces and the second not.

It's because they are a loop on a map: to find the 'bad' keys...

Upvotes: 1

Related Questions