Can any help me to creat the this program Write a Java program that outputs a li
ID: 3800404 • Letter: C
Question
Can any help me to creat the this program
Write a Java program that outputs a list of students from the given data file. Download the data file (student.txt) from our web site to the same folder as your program. You have to input the name of the data file from keyboard. You have to use input.hasNext() or input.hasNextInt() or input.hasNextDouble()with while loop when your program reads tokens from the file. We are assuming that you don’t know how many lines are in the data file. The file contains student id followed by name, gender and age. You also have to input (from keyboard) f or female if you want to print a list of female students, and m or male if you want print a list of male students. Before you print a list of male or female students, you should read all the student’s data from the text file (student.txt), and store the student’s info into an array. After this step, you should find and display a list of students from the array.
Your program outputs the student’s name and total number of students of the gender and their average age. For example if your data file contains the following data:
1 John m 18
2 William m 22 3 Susan f 21
4 Jack m 19
5 Jennifer f 18
Your program should produce the following output: What is the file name? student.dat
Which gender do you want? Female
List of Female Student ==================
Name age-
Susan 21
Jennifer 18
We have 2 female students. Average age is 19.5.
Student txt.
1 John m 18
2 William m 22
3 Susan f 21
4 Jack m 19
5 Jennifer f 18
6 Sophia f 21
7 Emma f 19
8 Olivia f 26
9 Ava f 23
10 Tom m 22
11 Mia f 18
12 Isabella f 29
13 Zoe f 17
14 Lily f 20
Explanation / Answer
StudentDataRead.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StudentDataRead {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan1 = new Scanner(System.in);
System.out.print("What is the file name? ");
String fileName = scan1.next();
File file = new File(fileName);
if(file.exists()){
System.out.print("Which gender do you want? ");
String gender = scan1.next();
char g='';
if(gender.equalsIgnoreCase("male")){
g = 'm';
}
else{
g='f';
}
int count = 0;
int sum = 0;
Scanner scan = new Scanner(file);
while(scan.hasNextLine()){
scan.nextInt();
String s = scan.next();
if(scan.next().charAt(0) == g){
count++;
System.out.println(s);
sum = sum + scan.nextInt();
}
else{
scan.nextInt();
}
}
double avergae = sum/(double)count;
System.out.println("We have "+count+" "+gender+" students.");
System.out.println("Average age of all student is "+avergae);
}
else{
System.out.println("File does not exist");
}
}
}
Output:
What is the file name? D:\student.dat
Which gender do you want? Female
Susan
Jennifer
We have 2 Female students.
Average age of all student is 19.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.