Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This Java programing focuses on reading input files, array structures, arraylist

ID: 3779940 • Letter: T

Question

This Java programing focuses on reading input files, array structures, arraylists and classes. You have to use the student class as shown below and ArrayList as we discussed in class. Be sure to include a short comment at the beginning of your program as well as a short comment for each method describing what it does.

Write a Java program that outputs a list of students from the given data file.The program prompts for file name and you have to input the name of the data file. Assume that you don’t know how many lines are in the data file. Therefore you have to use input.hasNext()or input.hasNextInt() or input.hasNextDouble()with a while loop so your program reads the tokens from the file. The file contains student names followed by gender and age. Your program should output each gender, the total number of students, each student’s id, name, and age by gender on the screen.

The program inculde student class.

// student class

class student {

String id;

String name;

String gender;

int 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

Out put ( Bold is user input)

What is the file name? student.txt

Female students: 2

ID Name Age

-----------------

3 Susan 21

5 Jennifer 18

Male students: 3

ID Name Age

-----------------

1 John 18

2 William 22

4 Jack 19

Explanation / Answer

Student.java

package file_handling;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Student {

   //Student Class with attributes
   private int id;
   private String name;
   private String gender;
   private int age;
  
   //Constructor to create new student
   public Student(int newId, String newName, String newGender, int newAge){
       id = newId;
       name = newName;
       gender = newGender;
       age = newAge;
   }
  
   public String getGender(){
       return gender;
   }
  
   //Method to print Student Details
   public void printDetails(){
       System.out.println(id + " " + name + " " + age);
   }
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       //Array List to store male Students
       ArrayList<Student> maleStudents = new ArrayList<>();
      
       //Array List to store female Students
       ArrayList<Student> femaleStudents = new ArrayList<>();
      
       try{
           Scanner lineReader = new Scanner(new File("/home/hrishi/workspace/File_handling/src/file_handling/input.txt"));
          
           //Read file line by line and store students in separate lists according to gender
           while(lineReader.hasNext()){
              
               String line = lineReader.nextLine();
               String[] parts = line.split(" ");
              
               int id = Integer.parseInt(parts[0]);
               String name = parts[1];
               String gender = parts[2];
               int age = Integer.parseInt(parts[3]);
              
               if(gender.equals("f")){
                   femaleStudents.add(new Student(id, name, gender, age));
               } else {
                   maleStudents.add(new Student(id, name, gender, age));
               }
              
           }
          
           lineReader.close();
       }
       catch(FileNotFoundException e){
           System.out.println("File Not Found!");
       }
      
       //Printing the details of all female students from file
       System.out.println("Female Students: " + femaleStudents.size());
       System.out.println("ID Name Age");
       for(Student student:femaleStudents){
           student.printDetails();
       }
      
       System.out.println("=========================================");
      
       //Printing the details of all female students from file
       System.out.println("Male Students: " + maleStudents.size());
       System.out.println("ID Name Age");
       for(Student student:maleStudents){
           student.printDetails();
       }
   }

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

input.txt

1 John m 18
2 William m 22
3 Susan f 21
4 Jack m 19
5 Jennifer f 18

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Output:

Female Students: 2
ID   Name   Age
3   Susan   21
5   Jennifer   18
=========================================
Male Students: 3
ID   Name   Age
1   John   18
2   William   22
4   Jack   19

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote