Solution: because the format of file is not given so based upon the format shown in figure I am considering the file in below format as input
InputTo Program
student.txt
Albert Einstein,56,89,90
Steve Abrew,45,78,56
Russel Peter,56,34
Sarah Trapp,23
Code:
package com.tree;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class StudentLinkedList {
Student head;
public void create(String fileName) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(fileName)));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
String[] array = line.split(“,”);
for (String data : array) {
// this is for very first node
if (head == null) {
Student student = new Student();
student.studentName = data;
head = student;
continue;
} // this for second node onward
else {
int data1 = 0;
try {
// because first word of file is name that means its a
// new student then in this case it wont be able to
// parse string then we can create student object in
// catch conditon and linked to previous student
data1 = Integer.parseInt(data);
// if this is first node the score wont be there then
// add it
if (head.score == null) {
Score score = new Score();
score.data = data1;
head.score = score;
} // if score is already added then link all score to
// other score for that student
else {
// this condition for very first student and linking
// all his score
Student nextStudent = head;
if (nextStudent.next == null) {
Score temp = nextStudent.score;
while (temp.next != null) {
temp = temp.next;
}
Score score = new Score();
score.data = data1;
temp.next = score;
} // this condition is for other students who are
// linked to first student onward and their
// score respectively
else {
while (nextStudent.next != null) {
nextStudent = nextStudent.next;
}
Score temp = nextStudent.score;
if (temp == null) {
Score score = new Score();
score.data = data1;
temp = score;
nextStudent.score = temp;
} else {
while (temp.next != null) {
temp = temp.next;
}
Score score = new Score();
score.data = data1;
temp.next = score;
}
}
}
} catch (Exception e) {
Student temp = head;
while (temp.next != null) {
temp = temp.next;
}
Student student = new Student();
student.studentName = data;
temp.next = student;
continue;
}
}
}
}
}
// this is used to display record
public void displayRecord() {
Student student = head;
while (student != null) {
System.out.print(student.studentName + “:”);
Score score = student.score;
while (score != null) {
System.out.print(score.data);
score = score.next;
if (score != null)
System.out.print(“,”);
}
System.out.println(“n”);
student = student.next;
}
}
static class Student {
String studentName;
Score score;
Student next;
}
static class Score {
Score next;
int data;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“Please provide file name with full path”);
String fileName = scanner.nextLine();
StudentLinkedList list = new StudentLinkedList();
try {
list.create(fileName);
list.displayRecord();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output
Please provide file name with full path
student.txt
Albert Einstein:56,89,90
Steve Abrew:45,78,56
Russel Peter:56,34
Sarah Trapp:23
if your file is outside workspace somewhere in the other directory then give full path as input including file name if you are working in eclipse then put your txt file inside the project as shown in below
![, practiseproject 4静src >串com.graph.matrix D>串com.tree JRE System Library [JavaSE-18] 目student.txt wordcount.txt wrestler.tt](https://media.cheggcdn.com/media%2F592%2F592b47b9-e677-4e16-8ebf-291e5da6e50a%2Fphpm4shGg.png)