This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Checking if a String value is already in a HashMap  RSS feed

 
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The user inputs their first name, then their exam mark. I am wanting to check if the mark of this student is already recorded, if it is then I will ask if they want to overwrite this. Should I be using something along the lines of this?:


This is my Class so far:

 
Saloon Keeper
Posts: 9869
199
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Marks are not the keys of the map, they're the values. If you want to see whether a student already have a mark, you have to check the map if it already contains their name as a key. Also, you need to negative your logic: Even if you replaced mark with name, you would be checking whether the map does NOT contain the name.

You should check the input == "Y" condition inside the if-statement above it.

Finally, don't declare variables before you need them, and use try-with-resources to close your resources when you're done with them.
 
Bartender
Posts: 678
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you should check the input == "Y" condition inside the if-statement above it.


This is an incorrect comparison. The variable `input` is of type char, so should be compared to 'Y', not "Y".
And if it were of type String, comparing using == is still incorrect.
And note that `input` is not in scope outside of the if-block.
 
Caiz Austin
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
We don't have time for this. We've gotta save the moon! Or check this out:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!