GeeCON Prague 2014*
The moose likes Java in General and the fly likes problem in hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "problem in hashmap" Watch "problem in hashmap" New topic
Author

problem in hashmap

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
Hello i am facing one problem in hashmap,
Here is the method:

if i am returning hashmap inside the loop, code works fine, but if if i return hashmap inside loop i need to return null outside the loop, otherwise it's a compile time error.
so basically i need to return hasmap outside the loop, but if i am doing that, values are repeating.

Here findFirstRowWithExactContent(string) is method which is checking for rows from the table

here is how i am calling it:


can anyone tell me what i am doing wrong?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

It's hard to read your first block of code, since you didn't format it...but I'll take a guess.

You have to return something for every possible execution path. So, the compiler says "well, what will it return if it never enters the loop?", doesn't see any return statement, and complains.

Note: If you are getting a compile error, I would strongly suggest you read what it says. It will tell you EXACTLY what is wrong. If you want us to help, please post the ENTIRE compiler error message here.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2

It's hard to read your first block of code, since you didn't format it...but I'll take a guess.

i am sorry, it was by mistake.


Note: If you are getting a compile error, I would strongly suggest you read what it says. It will tell you EXACTLY what is wrong. If you want us to help, please post the ENTIRE compiler error message here.

yes, it says i need to return hashmap.
so basically i am initializing hashmap outside the loop, but returning inside the loop, to get the desired output, that's why i need to return null outside the loop, and that's why it is showing me the compile time error.


You have to return something for every possible execution path. So, the compiler says "well, what will it return if it never enters the loop?", doesn't see any return statement, and complains.

it's entering inside the loop, but it's returning undesirable result.
something like this:

Value of String is: 5
Return Value is: 2 Content is: 30000
Return Value is: 2 Content is: 30000
Return Value is: 2 Content is: 30000
Return Value is: 2 Content is: 30000
Return Value is: 2 Content is: 30000
Final Values: [2]
Value of String is: 7
Return Value is: 0 Content is: 0000000
Return Value is: 0 Content is: 0000000
Return Value is: 0 Content is: 0000000
Return Value is: 0 Content is: 0000000
Return Value is: 0 Content is: 0000000


but the out which i was expecting would be something like this:

Value of String is: 5
Return Value is: 2 Content is: 30000
Final Values: [2]
Value of String is: 7
Return Value is: 0 Content is: 0000000


and this output i am getting when i am returning hasmap inside the loop.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Punit Jain wrote:
but the out which i was expecting would be something like this:

Value of String is: 5
Return Value is: 2 Content is: 30000
Final Values: [2]
Value of String is: 7
Return Value is: 0 Content is: 0000000


and this output i am getting when i am returning hasmap inside the loop.


If you were expecting this output, then I don't see any use of for loop in your code.
Actually, you are not even using the for loop iterator variable (iterate) anywhere in the loop (1st for loop).

So, just remove the loop.. And add a return statement at the end of the code..
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
i did the same earlier also R. Jain, but it didn't work but now it works, may be that time i did some mistake.
anyways thanks
 
GeeCON Prague 2014
 
subject: problem in hashmap