• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

comparing keys and values of a map

 
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to write a program that creates a hash map from a file then takes and asks the user to guess the capital of the state the state is the key the value is the capital. I also need to determine if the answer they gave of the capital to the state keep count if wrong or right which I haven't gotten to yet my problem is that I want to print a rand key/state then get the user to guess the capital.but when I try to get the key/state and print it prints null.Not sure why though because the hash map has values.

This is my code:

 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used this for testing as a data source:

I've identified the line with the problem and I know exactly why, and this is what I did.
I took this line

and made it several lines and printed their values:

The problem is now this line

According to the Java API, https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#get-java.lang.Object-

Class HashMap wrote:get
public V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.


The key value is not an int, but it is an object and in this case it is a String.
As a result your random int or any int will not work for you.
You cannot retrieve a value from a HashMap using an index like you would an array or ArrayList.

I'm not too sure how one goes about getting a random key from a HashMap.
However I suspect that people have asked that question before or something similar.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

problem is that I want to print a rand key/state


One way would be to save the name of the state in a List while loading the Map and then create a random index into the List.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line is also a problem: You probably want to use the equals method.  The == operator compares the objects' references, while the equals method compares the objects' contents.
 
Daniel Stallard
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norman radder so you're saying to make a list of the names of the states then make a map for the capitals?But how would I keep track of what state goes to what capital?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how would I keep track of what state goes to what capital?  


My suggestion was to add a List of states to the existing program and not to change what was already in the program.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My suggestion would be not to go further into the forest where you can easily get lost.
Current program looks messy enough already. I'd stop adding new features or fixing anything.

Instead - would clean it up by decomposing all that code into small methods, so one could at least follow it.

I don't see a lot benefit in doing it clearly in a wrong way. I think even complete starter can be taught to write programs in more organized, structured way.

All such comments needs to be pointed out - are usless and should not be written that way anymore.

@OP
Is it assignment?
 
Daniel Stallard
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Liutaras

Yes, it is an assignment I'm sorry that my code is messy.I'm no very familiar with using maps and it seems pretty complicated to get a random key.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

complicated to get a random key.


Not really.
Make a list of the keys
generate a random number in the range 0 to length of list -1
get the key at that location
 
Daniel Stallard
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was actually thinking about that list odea an i think correct me if i am wrong but all i need to do is add the states to a list then randamonly pick one from the list an print out key.getMap(key) where key/state equals list index/state
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Stallard wrote:. . . Yes, it is an assignment I'm sorry that my code is messy. . . .

Unfortunately, that will lose you marks.

I suggest you start by reading about Maps in the Java™ Tutorials. In the first instance try populating the Map by hand. Then consider populating it from the file, but that is a different task, and shou‍ld be done separately. I think you will find it much easier to write good code if you divide the assignment into smaller parts.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Stallard wrote:I'm sorry that my code is messy.


That is bad for you, not for me, so no worries

Daniel Stallard wrote:I'm no very familiar with using maps and it seems pretty complicated to get a random key.


It isn't about the map. It is about the environment where you fiddling with those maps. Everything around is messy.

As a real life example, imagine for a moment surgeon's scalpel, in your case it is a Map, but have you ever had a chance to see how the things are organised in a surgery room?
[1] Everything have their precise place
[2] All the time they are in the same place
[3] There are no unnecessary things

That is all about that.

Now think if everything would be like in a flea market, I think you wouldn't like even to think about the idea in having any kind of business with such surgery room.

Program's organisation and slight pedanticism isn't something you need to study for years in order to learn. I think this is something you can pick up right away and help yourself a lot.

All the comments you wrote - they add extra noise and disturb you from seeing actual code.

Example of useless comment you wrote:

As you see it adds absolutely no extra information to what actual code line tells you. So why to add it?

However, in your code there is only one comment which in my opinion is quite useful:

What it tells me as a reader, that it is your intention to do nothing after you catch the exception at line 67. If such comment wasn't there, I'd think you might forgot to add handling part. As an improvement of this comment you could write why there is an empty handling part.

So, only by removing all useless comments, your program would look way better. That's one.

Second. Variable names.


As you see, you trade in clarity for saving the keystroke - that's a bad idea. You should pay attention to clarity, always. Imagine you have a big project and you are discussing with your fellow colleague about the code snippet where state capitals are handled. So you ask him to have a look. How one could know how to find that part? In IDE's there are tools to look up for text match within a project, and if you were name it stateCapital or other sensible name which you use in spoken language with your fellow colleague, that would be very easy for him/her to find it. You don't tell him about the code, look, there is a problem with sta cap map. What?!? I mean there is a problem with state capital... Oh, this is what you mean... So why not to use clear, unambiguous names?

Third. Methods.

One of your methods is 50 lines long. I have in mind readText(). You could break it down slightly, so you'd have 2 or 3 methods out of this 1. The good part about breaking down problem into smaller problems is, that if you know that there is a problem in adding capitals to some kind of data structure, you are worrying only about the method (small), which is responsible for that, rather than searching through all 50 lines and thinking if there are no overlapping logic or similar. Deal with small things is always easier than with bigger. Unless you are aiming with a bow at target

I'm not forcing or giving you a pressure, but just think it through, you might will find some ideas useful.


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic