Score Entry and Query: Student Chinese Score System
Introduction
Build a console score management tool: enter student names and Chinese scores, query by name, and find highest/lowest scores. You will combine HashMap, Scanner, Loops, and Conditionals into one practical workflow.
Prerequisites
- HashMap
- Menu loops and input validation basics
Project Goal
The program supports:
- Add or update a student’s Chinese score
- Query score by name
- Show highest score among recorded students
- Show lowest score among recorded students
- Show all records (bonus)
- Exit
1) Data Structure
Map<String, Integer> scores = new HashMap<>();- Key: student name (
String) - Value: Chinese score (
Integer)
Duplicate names overwrite previous score—use student IDs in real systems if names can collide.
2) Core Methods
Tip
Stream Alternative
With Streams:
scoreMap.entrySet().stream().max(Map.Entry.comparingByValue()) — same idea, fewer lines when you are ready.
3) Run the Program
Save as ScoreEntryAndQuery.java, compile, and run from IntelliJ or the terminal.
Surprise Upgrade: Ranking Option
Add menu item 6. Show ranking (high to low):
- Copy
entrySetto aList<Map.Entry<String,Integer>> - Sort by value descending
- Print rank, name, score
- Label top 3: Gold / Silver / Bronze
You are building a mini grade system—not just a demo.
Common Beginner Mistakes
max/min on Empty Map
Always check isEmpty() first.
Not Consuming Newline After nextInt
Call nextLine() after numeric input before next nextLine() for names.
Same Name Twice
Later entry overwrites—document or use unique IDs.
What’s Next
Unique values with HashSet, then Deduplicate and Sort Scores.
FAQ
Why HashMap instead of ArrayList?
O(1) average lookup by name. Lists require scanning every row.
Can one student have multiple subjects?
Use Map<String, Map<String, Integer>> or a small record value type (advanced).
How to persist data after exit?
Save to JSON or file (JSON Handling, File Handling).
Why validate 0–100?
Keeps reports meaningful and catches typos early.
Integer vs int in Map?
Map<String, Integer> is standard; autoboxing handles primitives when putting int.