The Java program uses a Scanner to collect student data by gender, age and count the totals. It prompts the user to enter student details like gender and age, and categorizes them as male, female, minor, adult or senior. Finally it displays the total count of students in each category and determines if there are more male or female students.
StudentData.javaimport java.util.Scanner; public class Student.pdf
1. StudentData.java
import java.util.Scanner;
public class StudentData {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int totalStudents = 0;
int maleCount = 0, femaleCount = 0, minorCount = 0, adultCount = 0, seniorCount = 0;
while(true){
System.out.println("Do you ant to add a student (n to stop):");
char ch = scan.next().charAt(0);
if(ch == 'n' || ch == 'N'){
break;
}
else{
System.out.println("Enter the student gender (male or female)");
String gender = scan.next();
if(gender.equalsIgnoreCase("male")){
maleCount++;
}
else{
femaleCount++;
}
System.out.println("Enter student age:");
int age = scan.nextInt();
if(age <= 18){
minorCount++;
}
else if(age >=19 && age <= 65){
adultCount++;
}
else{
seniorCount++;
}
2. }
}
totalStudents = maleCount + femaleCount;
System.out.println("Total Students: "+totalStudents);
System.out.println("Male: "+maleCount);
System.out.println("Female: "+femaleCount);
System.out.println("Minor: "+minorCount);
System.out.println("Adult: "+adultCount);
System.out.println("Senior: "+seniorCount);
String s = (maleCount >= femaleCount)? "Students are mre males":"Students are more
females";
System.out.println(s);
}
}
Output:
Do you ant to add a student (n to stop):
y
Enter the student gender (male or female)
male
Enter student age:
44
Do you ant to add a student (n to stop):
y
Enter the student gender (male or female)
male
Enter student age:
77
Do you ant to add a student (n to stop):
y
Enter the student gender (male or female)
male
Enter student age:
12
Do you ant to add a student (n to stop):
yy
Enter the student gender (male or female)
3. female
Enter student age:
55
Do you ant to add a student (n to stop):
y
Enter the student gender (male or female)
female
Enter student age:
33
Do you ant to add a student (n to stop):
n
Total Students: 5
Male: 3
Female: 2
Minor: 1
Adult: 3
Senior: 1
Students are mre males
Solution
StudentData.java
import java.util.Scanner;
public class StudentData {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int totalStudents = 0;
int maleCount = 0, femaleCount = 0, minorCount = 0, adultCount = 0, seniorCount = 0;
while(true){
System.out.println("Do you ant to add a student (n to stop):");
char ch = scan.next().charAt(0);
if(ch == 'n' || ch == 'N'){
break;
}
4. else{
System.out.println("Enter the student gender (male or female)");
String gender = scan.next();
if(gender.equalsIgnoreCase("male")){
maleCount++;
}
else{
femaleCount++;
}
System.out.println("Enter student age:");
int age = scan.nextInt();
if(age <= 18){
minorCount++;
}
else if(age >=19 && age <= 65){
adultCount++;
}
else{
seniorCount++;
}
}
}
totalStudents = maleCount + femaleCount;
System.out.println("Total Students: "+totalStudents);
System.out.println("Male: "+maleCount);
System.out.println("Female: "+femaleCount);
System.out.println("Minor: "+minorCount);
System.out.println("Adult: "+adultCount);
System.out.println("Senior: "+seniorCount);
String s = (maleCount >= femaleCount)? "Students are mre males":"Students are more
females";
System.out.println(s);
}
}
Output:
Do you ant to add a student (n to stop):
5. y
Enter the student gender (male or female)
male
Enter student age:
44
Do you ant to add a student (n to stop):
y
Enter the student gender (male or female)
male
Enter student age:
77
Do you ant to add a student (n to stop):
y
Enter the student gender (male or female)
male
Enter student age:
12
Do you ant to add a student (n to stop):
yy
Enter the student gender (male or female)
female
Enter student age:
55
Do you ant to add a student (n to stop):
y
Enter the student gender (male or female)
female
Enter student age:
33
Do you ant to add a student (n to stop):
n
Total Students: 5
Male: 3
Female: 2
Minor: 1
Adult: 3