• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in hashmap

 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic