002-001-003

Personal Profile Card Generator

Easy

Problem Description

[Explanation]

In this problem, you will create a program that reads the user's name, age, and hobby from keyboard input and displays them as a decorated personal profile card to standard output.

1. Problem Background and Purpose

This problem is designed to teach the fundamental concept of "keyboard input" in Java programming. In real programs, it's common to receive information from users, process it, and display results. Through this problem, you will master the basics of input processing using the Scanner class.

Additionally, by creating visually appealing output, you can experience the joy of programming and gain confidence that "I can create useful programs too."

This learning directly connects to the next steps of "conditional branching" and "loops." This is because changing processing based on input values (conditional branching) and receiving multiple inputs (loops) are fundamental programming patterns.

2. Prerequisites in Detail

To solve this problem, you need the following foundational knowledge:

What are Variables

Variables are "named boxes" for temporarily storing data during program execution. For example, if you want to use the name entered by the user later, you save that name in a variable. Variables are named in the form "data_type variable_name" like "String name."

  • String type: Type for storing strings (text). Examples: "Alice", "Reading"
  • int type: Type for storing integers. Examples: 25, 30, 0

What is the Scanner class

Scanner is a convenient tool (class) for receiving input from the keyboard. Using it, you can easily read strings and numbers entered by users.

What is System.in

System.in is a special variable representing "standard input," which typically refers to keyboard input. By writing Scanner(System.in), it means "create a Scanner that reads input from the keyboard."

3. Detailed Code Explanation Line by Line

Import Statement

import java.util.Scanner;

This line is an "import statement" to make the Scanner class available. Since Scanner is included in the java.util package, this declaration is necessary.

Creating Scanner Object

Scanner sc = new Scanner(System.in);

This line creates a Scanner object for reading keyboard input.

  • sc: Name of the Scanner variable (sc is commonly used as an abbreviation for "scanner")
  • new Scanner(System.in): Creates a new Scanner object and sets it to receive input from System.in (keyboard)

Reading Name

String name = sc.nextLine();

This line reads one line of text entered by the user and stores it in the variable name.

  • sc.nextLine(): Calls the Scanner's nextLine() method to get input as a string until the user presses Enter
  • At this point, the variable name contains the entered string (e.g., "Alice")

Reading Age

int age = sc.nextInt();
sc.nextLine(); // Skip newline

The first line reads the integer entered by the user and stores it in the variable age.

  • sc.nextInt(): Method for reading integers
  • Important: nextInt() only reads the number and leaves the newline character behind. Therefore, if you call nextLine() next, it would read that remaining newline instead of the actual input. To prevent this, call sc.nextLine() once extra to consume the leftover newline.

Validating Age

if (age < 0) {
    System.out.println("Error: Age must be zero or positive");
    return;
}

This checks whether the entered age is a negative number. If it is, an error message is displayed and the program exits. This is an important process called "input validation."

Reading Hobby

String hobby = sc.nextLine();

Similar to name, this reads one line of text and stores it in the variable hobby.

Displaying the Profile Card

System.out.println("=========================");
System.out.println("PERSONAL PROFILE CARD");
System.out.println("=========================");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Hobby: " + hobby);
System.out.println("=========================");

Using the stored variable values, this displays a visually appealing profile card.

  • + operator: Concatenates strings. "Name: " + name joins the string "Name: " with the value of the name variable
  • Lining up "=" characters creates top and bottom borders for the card, making it easy to read

4. Common Mistakes and Corrections

Mistake 1: Forgetting nextLine() after nextInt()

// Wrong example
int age = sc.nextInt();
String hobby = sc.nextLine(); // hobby becomes an empty string

Reason: nextInt() only reads the number and leaves the newline character. As a result, the next nextLine() reads that empty line instead.

Correct code:

int age = sc.nextInt();
sc.nextLine(); // Skip the leftover newline
String hobby = sc.nextLine(); // hobby is read correctly

Mistake 2: Forgetting to import Scanner

// Wrong example (no import statement)
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); // Compile error
    }
}

Correct code: Add import java.util.Scanner; at the top of the file.

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up