• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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: 9134
172
  • 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: 619
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!