Win a copy of Spring in Action (5th edition) this week in the Spring 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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:

 
Bartender
Posts: 9493
184
  • 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: 634
9
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!