• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

problem in hashmap

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13072
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic