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

For this part of the assignment you will need to write some code that will parse

ID: 3715857 • Letter: F

Question

For this part of the assignment you will need to write some code that will parse a human-readable formatted text file. You can use the Scanner class to read the file The file has the following format: Name,ID,GPA,Course1,Course2,...,

courseN Name - A String type representing a student's name

ID - An int type representing a student's ID number

GPA - A double type representing a student's GPA

Course - A String type representing a course the student is enrolled in.

Note that there may be multiple courses. Files may contain multiple lines following this format. Your code should perform the following actions:

1) Open the file specified by the parameter fileName for reading. * If the file cannot be found, return an empty ArrayList containing no students.

2) Parse the file until you run out of input.

3) For every line of the file, construct a new Student object and add it to an ArrayList that you will return later. * The Student constructor is of takes the following arguments (String, int, double, String[]) in that order.

* If the file does not follow the specified format above, throw a new InputMismatchExcpetion with the message "Input file is malformed."

* Note: You may need to catch an exception of this type first before throwing your own. * Note: You may want to use the split method as part of your solution as it is easier than other approaches.

4) Return the ArrayList of Students.

I want the exactly code of 3) and 4). I don't know how o return

Explanation / Answer

package file;

import java.io.*;

import java.util.*;

import java.util.regex.*;

// Class InputMismatchExcpetion definition for exception handling

class InputMismatchExcpetion

{

// Parameterized constructor to display error message

InputMismatchExcpetion(String message)

{

System.out.println("Error: " + message);

}// End of constructor

}// End of class

// Class Student definition

class Student

{

// Instance variable to store student data

String name;

int ID;

double GPA;

String course[];

// Parameterized constructor to initializes data member

Student(String na, int id, double gpa, String cor[])

{

// Parameter data is assigned to instance variables

name = na;

ID = id;

GPA = gpa;

course = cor;

}// End of constructor

// Overrides toString() to return student information

public String toString()

{

return " Name: " + name + " ID: " + ID + " GPA: " + GPA + " Courses: " + course[0] + ", " + course[1] + ", " + course[2];

}// End of method

}// End of class

// Driver class CourseArrayList definition

public class CourseArrayList

{

// Creates an array list for students

ArrayList <Student> students = new ArrayList<Student>();

void readFile()

{

// Local variables to store data read from file

int ID;

double GPA;

String course[] = new String[3];

String inf[] = null;

int c = 0;

// Creates a pattern for number

Pattern intP = Pattern.compile("([0-9])");

Matcher m;

// try block for file not found

try

{

// Creates scanner class object to open file for reading

Scanner sc = new Scanner(new File("StudentInfo.txt"));

// Loops till end of the record

while(sc.hasNextLine())

{

// try block for check validity of data read from file

try

{

// Reads a student record

String data = sc.nextLine();

// Splits the record by space and stores it in String inf array

inf = data.split(" ");

// Checks the name length if it is zero then throw an exception

if(inf[0].length() == 0)

new InputMismatchExcpetion("Name cannot be balnk.");

// Stores the pattern of ID for matching

m = intP.matcher(inf[1]);

// Checks for the pattern if not found then generate exception

if(!m.find())

new InputMismatchExcpetion("Invalid ID: " + inf[1]);

// Otherwise convert the inf[1] to integer and store it in ID

ID = Integer.parseInt(inf[1]);

// Stores the pattern of GPA for matching

m = intP.matcher(inf[2]);

// Checks for the pattern if not found then generate exception

if(!m.find())

new InputMismatchExcpetion("Invalid GPA: " + inf[2]);

// Otherwise convert the inf[2] to integer and store it in GPA

GPA = Double.parseDouble(inf[2]);

// Stores the course names

course[0] = inf[3];

course[1] = inf[4];

course[2] = inf[5];

// Creates an anonymous object using parameterized constructor and adds it to array list

students.add(new Student(inf[0], ID, GPA, course));

// Increase the record counter by one

c++;

}// End of inner try block

// Catch block to handle number format exception

catch(NumberFormatException ne)

{

// Displays error message with invalid record number

System.out.println("Invalid data found! Record No: " + (c + 1));

}// End of catch block

}// End of while

// Close the file

sc.close();

}// End of outer try block

// Catch block to handle file not found exception

catch(FileNotFoundException fe)

{

System.out.println("File not foune!");

}// End of catch block

}// End of method

// Method to display student information

void displayStudent()

{

// Loops till end of array list and displays student information

for(int x = 0; x < students.size(); x++)

System.out.println(students.get(x));

}// End of method

// main function definition

public static void main(String[] args)

{

// Creates an object of class CourseArrayList

CourseArrayList cl = new CourseArrayList ();

// Calls the method to read file

cl.readFile();

// Calls the method to display student information

cl.displayStudent();

}// End of main method

}// End of class

Sample Output:

Error: Invalid ID: s

Invalid data found! Record No: 2

Error: Invalid GPA: t

Invalid data found! Record No: 3

Name: Pyri ID: 101 GPA: 89.22 Courses: AI, CS, OS

Name: Sahu ID: 103 GPA: 99.52 Courses: AI, CS, OS

Name: Amit ID: 105 GPA: 79.88 Courses: AI, CS, OS

Name: Binod ID: 106 GPA: 89.81 Courses: AI, CS, OS

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