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

java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
 
public class ScoreEntryAndQuery {
 
    private static void addScore(Map<String, Integer> scoreMap, Scanner scanner) {
        System.out.print("Enter student name: ");
        String name = scanner.nextLine().strip();
        if (name.isEmpty()) {
            System.out.println("Name cannot be empty.");
            return;
        }
 
        System.out.print("Enter Chinese score: ");
        if (!scanner.hasNextInt()) {
            System.out.println("Invalid score. Please enter an integer.");
            scanner.next();
            scanner.nextLine();
            return;
        }
 
        int score = scanner.nextInt();
        scanner.nextLine();
 
        if (score < 0 || score > 100) {
            System.out.println("Score must be between 0 and 100.");
            return;
        }
 
        scoreMap.put(name, score);
        System.out.println("Saved: " + name + " -> " + score);
    }
 
    private static void queryScore(Map<String, Integer> scoreMap, Scanner scanner) {
        System.out.print("Enter student name to query: ");
        String name = scanner.nextLine().strip();
 
        if (!scoreMap.containsKey(name)) {
            System.out.println("Student not found.");
            return;
        }
 
        System.out.println(name + "'s Chinese score: " + scoreMap.get(name));
    }
 
    private static void showHighest(Map<String, Integer> scoreMap) {
        if (scoreMap.isEmpty()) {
            System.out.println("No records yet.");
            return;
        }
 
        String topName = null;
        int topScore = Integer.MIN_VALUE;
 
        for (Map.Entry<String, Integer> entry : scoreMap.entrySet()) {
            if (entry.getValue() > topScore) {
                topScore = entry.getValue();
                topName = entry.getKey();
            }
        }
 
        System.out.println("Highest score: " + topName + " -> " + topScore);
    }
 
    private static void showLowest(Map<String, Integer> scoreMap) {
        if (scoreMap.isEmpty()) {
            System.out.println("No records yet.");
            return;
        }
 
        String lowName = null;
        int lowScore = Integer.MAX_VALUE;
 
        for (Map.Entry<String, Integer> entry : scoreMap.entrySet()) {
            if (entry.getValue() < lowScore) {
                lowScore = entry.getValue();
                lowName = entry.getKey();
            }
        }
 
        System.out.println("Lowest score: " + lowName + " -> " + lowScore);
    }
 
    private static void showAll(Map<String, Integer> scoreMap) {
        if (scoreMap.isEmpty()) {
            System.out.println("No records yet.");
            return;
        }
 
        System.out.println("=== All Student Scores ===");
        scoreMap.forEach((name, score) -> System.out.println(name + ": " + score));
    }
 
    public static void main(String[] args) {
        Map<String, Integer> scores = new HashMap<>();
 
        try (Scanner scanner = new Scanner(System.in)) {
            while (true) {
                System.out.println("\n=== Student Chinese Score System ===");
                System.out.println("1. Add/Update score");
                System.out.println("2. Query score by name");
                System.out.println("3. Show highest score");
                System.out.println("4. Show lowest score");
                System.out.println("5. Show all records");
                System.out.println("0. Exit");
                System.out.print("Choose an option: ");
 
                String choice = scanner.nextLine().strip();
 
                switch (choice) {
                    case "1" -> addScore(scores, scanner);
                    case "2" -> queryScore(scores, scanner);
                    case "3" -> showHighest(scores);
                    case "4" -> showLowest(scores);
                    case "5" -> showAll(scores);
                    case "0" -> {
                        System.out.println("Great work — bye!");
                        return;
                    }
                    default -> System.out.println("Invalid option. Try again.");
                }
            }
        }
    }
}

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.