Java Variables and Data Types Challenge: Build a Simple Student Profile
• Updated 3/9/2026 • javavariablesdata typesbeginner coding challengestringintdoublebooleanscanner
Problem statement
A school wants a simple program that can display a student's basic profile on the screen. Your task is to create a Java program that asks the user for a student's name, age, grade average, and whether the student is currently enrolled.
The program must store each value using an appropriate data type and then display the full student profile in a clear format. If the user enters invalid data for age, grade average, or enrollment status, the program should show a clear error message.
Input and output expectations
- Input: student name as text
- Input: age as a whole number
- Input: grade average as a decimal number
- Input: enrollment status as true or false
- Output: a formatted student profile showing all entered values
- Output: an error message if age, grade average, or enrollment status is invalid
Constraints
- Do not use file input or file output
- Do not use databases or network requests
- Age must be between 1 and 120
- Grade average must be between 0 and 100
- Enrollment status must be either true or false
- Use only beginner-friendly Java concepts
Hints
- Use a String for the student's name
- Use an int for the age
- Use a double for the grade average
- Use a boolean for the enrollment status
- Validate user input before showing the final profile
Solution explanation
First, ask the user to enter the student's name and store it in a string variable. Then ask for the age and check that the input is a whole number before storing it in an integer variable.
Next, ask for the grade average and make sure it is a decimal or whole number. Store it in a double variable. After that, ask whether the student is enrolled and check that the answer is either true or false before storing it in a boolean variable.
Once all values are valid, print the student profile in a readable format. This challenge helps you practice choosing the correct data type for different kinds of information.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter student name: ");
String studentName = scanner.nextLine().trim();
if (studentName.isEmpty()) {
System.out.println("Error: Name cannot be empty.");
return;
}
System.out.print("Enter age: ");
if (!scanner.hasNextInt()) {
System.out.println("Error: Age must be a whole number.");
return;
}
int age = scanner.nextInt();
if (age < 1 || age > 120) {
System.out.println("Error: Age must be between 1 and 120.");
return;
}
System.out.print("Enter grade average: ");
if (!scanner.hasNextDouble()) {
System.out.println("Error: Grade average must be a number.");
return;
}
double gradeAverage = scanner.nextDouble();
if (gradeAverage < 0 || gradeAverage > 100) {
System.out.println("Error: Grade average must be between 0 and 100.");
return;
}
System.out.print("Is the student enrolled? (true/false): ");
if (!scanner.hasNextBoolean()) {
System.out.println("Error: Enrollment status must be true or false.");
return;
}
boolean isEnrolled = scanner.nextBoolean();
System.out.println();
System.out.println("Student Profile");
System.out.println("Name: " + studentName);
System.out.println("Age: " + age);
System.out.printf("Grade Average: %.2f%n", gradeAverage);
System.out.println("Enrolled: " + isEnrolled);
}
}
/*
Sample Output:
Enter student name: Maria
Enter age: 18
Enter grade average: 89.5
Is the student enrolled? (true/false): true
Student Profile
Name: Maria
Age: 18
Grade Average: 89.50
Enrolled: true
*/
Variations or next challenges
- Add a character variable for the student's section letter
- Display whether the student passed based on the grade average
- Ask for two students and compare their grade averages
- Format the output as a small report card
- Calculate how many years remain before the student turns 21
Related snippets or guides
- Java variables
- Primitive data types
- String values
- Boolean values
- User input with Scanner
- Basic input validation
- Formatted output with printf

