Write a Java program that outputs a list of students from th

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 in the data file. The file contains student name followed by 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. 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 outpu(Bold Character is user\'s input):

What is the file name? student.dat

Which gender do you want? female

Susan

Jennifer

We have 2 female students.

Average age of all student is 19.5

Solution

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=\'\\0\';
           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

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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site