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:

  1. Add or update a student’s Chinese score
  2. Query score by name
  3. Show highest score among recorded students
  4. Show lowest score among recorded students
  5. Show all records (bonus)
  6. Exit

1) Data Structure

java
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):

  1. Copy entrySet to a List<Map.Entry<String,Integer>>
  2. Sort by value descending
  3. Print rank, name, score
  4. 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.