• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ranch Hand
Posts: 35
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: 8724
162
  • 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: 608
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: 35
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!