Keep getting error message for this JAVA program. I would like for the program t
ID: 3831176 • Letter: K
Question
Keep getting error message for this JAVA program. I would like for the program to search and display names based on the first letter of the first names. I already have a student.java that I use also for another sorting program but it doesn't want to work for this sorting program as well.
-----------------------------
Assignment:
Create a class StudentFinder that reads in a list of students from a file. The Student.java class will contain name, ID, and GPA. The user will be prompted to enter the first letter of a student's name. Then by using Java 8 Streams, filter the list of students based on the user's input and print the filtered list.
-------------------------
Harper Lee 4.5 1111
Donald Miller 4.0 2222
Patrick Gibbels 4.5 3333
Edgar Poe 4.0 4444
Clive Lewis 3.5 5555
J.k. Rowling 3.0 6666
----------------------------------------
import java.io.*;
import java.util.*;
public class Student
{
private String firstname;
private String lastname;
private double GPA;
private int id;
public Student(String firstname, String lastname, double gPA, int id)
{
super();
this.firstname = firstname;
this.lastname = lastname;
GPA = gPA;
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Student [firstname=" + firstname + ", lastname=" + lastname
+ ", GPA=" + GPA + ", id=" + id + "]";
}
}
-----------------------
import java.io.*;
import java.util.*;
public class StudentFinder
{
public static void main(String[] args) throws IOException
{
BufferedReader studentsBuffer = null;
boolean flag = false;
//String fname;
//String lname;
ArrayList<Student> arl = new ArrayList<Student>();
try
{
String studentsLine;
studentsBuffer = new BufferedReader(new FileReader("studentinfo"));
while ((studentsLine = studentsBuffer.readLine()) != null)
{
addStudent(arl,studentsLine);
}
studentsBuffer = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter first letter of students name first name :");
char firstletter = studentsBuffer.readLine().charAt(0);
for(int i = 0; i < arl.size(); ++i)
{
Student s = arl.get(i);
if (Character.toLowerCase(firstletter) == (s.getFirstname().trim().toLowerCase().charAt(0)))
{
flag = true;
System.out.println("Student's First Name : " + s.getFirstname() +
"Student's Last Name : " + s.getLastname() +
"Student's ID : "+ s.getId() +
"Student's GPA : " + s.getGPA());
}
}
if(!flag)
System.out.println("There is no student that begins with the letter " + firstletter + " please select a letter and resubmit");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
try
{
if(studentsBuffer != null) studentsBuffer.close();
}
catch (IOException studentsException)
{
studentsException.printStackTrace();
}
}
}
public static void addStudent(ArrayList<Student>al ,String studentCSV)
{
String tokens[] = studentCSV.split(",");
String fname[] = tokens[0].split(" ");
//String lname[] = tokens[1].split(" ");
Student s = new Student(
names[0].trim(),
names[1].trim(),
// String.parseString(tokens[3].trim()),
Double.parseDouble(tokens[2].trim()),
Integer.parseInt(tokens[1].trim()));
al.add(s);
}
}
Explanation / Answer
please find the correct code
package pckg1;
import java.io.*;
import java.util.*;
public class StudentFinder
{
public static void main(String[] args) throws IOException
{
BufferedReader studentsBuffer = null;
boolean flag = false;
//String fname;
//String lname;
ArrayList<Student> arl = new ArrayList<Student>();
try
{
String studentsLine;
studentsBuffer = new BufferedReader(new FileReader("C:/Users/anmamaha/Desktop/student.txt"));
while ((studentsLine = studentsBuffer.readLine()) != null)
{
addStudent(arl,studentsLine);
}
studentsBuffer = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter first letter of students name first name :");
char firstletter = studentsBuffer.readLine().charAt(0);
for(int i = 0; i < arl.size(); ++i)
{
Student s = arl.get(i);
if (Character.toLowerCase(firstletter) == (s.getFirstname().trim().toLowerCase().charAt(0)))
{
flag = true;
System.out.println("Student's First Name : " + s.getFirstname() +
"Student's Last Name : " + s.getLastname() +
"Student's ID : "+ s.getId() +
"Student's GPA : " + s.getGPA());
}
}
if(!flag)
System.out.println("There is no student that begins with the letter " + firstletter + " please select a letter and resubmit");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
try
{
if(studentsBuffer != null) studentsBuffer.close();
}
catch (IOException studentsException)
{
studentsException.printStackTrace();
}
}
}
public static void addStudent(ArrayList<Student>al ,String studentCSV)
{
String tokens[] = studentCSV.split(",");
String fname[] = tokens[0].split(" ");
//String lname[] = tokens[1].split(" ");
Student s = new Student(
fname[0].trim(),
fname[1].trim(),
// String.parseString(tokens[3].trim()),
Double.parseDouble(tokens[1].trim()),
Integer.parseInt(tokens[2].trim())
);
al.add(s);
}
}
package pckg1;
import java.io.*;
import java.util.*;
public class Student
{
private String firstname;
private String lastname;
private double GPA;
private int id;
public Student(String firstname, String lastname, double gPA, int id)
{
super();
this.firstname = firstname;
this.lastname = lastname;
GPA = gPA;
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Student [firstname=" + firstname + ", lastname=" + lastname
+ ", GPA=" + GPA + ", id=" + id + "]";
}
}
content of student.txt
Harper Lee,4.5,1111
Donald Miller,4.0,2222
Patrick Gibbels,4.5,3333
Edgar Poe,4.0,4444
Clive Lewis,3.5,5555
J.k. Rowling,3.0,6666
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.