Java Variables and Data Types Challenge: Build a Simple Student Info Formatter
• Updated 3/8/2026 • javavariablesdata typesjava basicsbeginner coding challengescanneruser inputdoubleintstring
1. Problem statement
Create a Java program that asks the user to enter a student's name, age, Math score, and Science score. The program should store each value using the correct variable and data type, then display a neat summary of the student's information along with the average of the two scores.
This challenge helps beginners practice choosing the right data types, working with variables, accepting user input, and performing a simple calculation.
2. Input and output expectations
- Input:
- Student name as text
- Age as a whole number
- Math score as a decimal or whole number
- Science score as a decimal or whole number
- Output:
- Student name
- Age
- Math score
- Science score
- Average score
If the input is invalid, the program should show a clear error message.
3. Constraints
- Use Java only.
- Do not use file input or file output.
- Do not use databases or network requests.
- Validate user input before using it.
- Age must be greater than 0.
- Scores must be between 0 and 100.
- Keep the solution simple and beginner-friendly.
4. Hints
- Use String for the student's name.
- Use int for age.
- Use double for scores and the average.
- Use Scanner to read user input.
- Use hasNextInt() and hasNextDouble() to validate numeric input.
- Calculate the average by adding the two scores and dividing by 2.0.
5. Solution explanation
The program starts by asking the user to enter the student's name. Since a name is text, it is stored in a String variable.
Next, it asks for the student's age. Because age is a whole number, it is stored in an int variable. The program checks that the input is a valid integer and that it is greater than 0.
Then it asks for the Math and Science scores. These are stored as double values so the program can accept both whole numbers and decimal values. Each score is checked to make sure it is between 0 and 100.
After all values are valid, the program calculates the average score using a double variable. Finally, it prints a clean summary showing all the stored values.
This challenge shows how different data types work together in a real program.
6. Solution code
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 name = scanner.nextLine().trim();
if (name.isEmpty()) {
System.out.println("Error: Name cannot be empty.");
scanner.close();
return;
}
System.out.print("Enter age: ");
if (!scanner.hasNextInt()) {
System.out.println("Error: Age must be a valid whole number.");
scanner.close();
return;
}
int age = scanner.nextInt();
if (age <= 0) {
System.out.println("Error: Age must be greater than 0.");
scanner.close();
return;
}
System.out.print("Enter Math score: ");
if (!scanner.hasNextDouble()) {
System.out.println("Error: Math score must be a valid number.");
scanner.close();
return;
}
double mathScore = scanner.nextDouble();
if (mathScore < 0 || mathScore > 100) {
System.out.println("Error: Math score must be between 0 and 100.");
scanner.close();
return;
}
System.out.print("Enter Science score: ");
if (!scanner.hasNextDouble()) {
System.out.println("Error: Science score must be a valid number.");
scanner.close();
return;
}
double scienceScore = scanner.nextDouble();
if (scienceScore < 0 || scienceScore > 100) {
System.out.println("Error: Science score must be between 0 and 100.");
scanner.close();
return;
}
double average = (mathScore + scienceScore) / 2.0;
System.out.println();
System.out.println("Student Summary");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Math Score: " + mathScore);
System.out.println("Science Score: " + scienceScore);
System.out.printf("Average Score: %.2f%n", average);
scanner.close();
}
}
/*
Sample Output:
Enter student name: Anna
Enter age: 16
Enter Math score: 89.5
Enter Science score: 92
Student Summary
Name: Anna
Age: 16
Math Score: 89.5
Science Score: 92.0
Average Score: 90.75
*/
7. Variations or next challenges
- Add a third subject score and calculate a new average.
- Display whether the student passed or failed based on the average.
- Convert the average score into a letter grade.
- Ask for height and weight, then print a simple personal profile.
8. Related snippets or guides
- Declaring variables in Java
- Understanding String, int, and double
- Reading input with Scanner
- Formatting decimal output with printf
- Basic input validation in console programs

