Help Error code in JAVA Program! (In the student.java it includes a last name bu
ID: 3831152 • Letter: H
Question
Help Error code in JAVA Program!
(In the student.java it includes a last name but this student searcher should allow the user to submit one letter and display all first names starting with that letter)
------------------------------------------
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
§ at StudentFinder.addStudent(StudentFinder.java:115)
§ at StudentFinder.main(StudentFinder.java:33)
§
-----------------------------
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(
fname[0].trim(),
//lname[1].trim(),
// String.parseString(tokens[3].trim()),
Double.parseDouble(tokens[2].trim()),
Integer.parseInt(tokens[1].trim()));
al.add(s);
}
}
Explanation / Answer
You may get ArrayIndexOutOfBoundException if student doesnot have any lastname and after spliting with space you are trying to access the last name.
I have added the firstname lastname set code in the Student class constructor here.
code:
package chegg;
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\IBM_ADMIN\workspace\testnew\src\chegg\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)
{
System.out.println(studentCSV);
String tokens[] = studentCSV.split(",");
//String fname[] = tokens[0].split(" ");
//String lname[] = tokens[1].split(" ");
Student s = new Student(
tokens[0].trim(),
//lname[1].trim(),
// String.parseString(tokens[3].trim()),
Double.parseDouble(tokens[2].trim()),
Integer.parseInt(tokens[1].trim()));
al.add(s);
}
}
class Student{
String firstname;
String lastname;
int Id;
double GPA;
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 int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public double getGPA() {
return GPA;
}
public void setGPA(double GPA) {
this.GPA = GPA;
}
Student(String name, double d, int i){
String[] names=name.split(" ");
firstname=names[0];
if(names.length>1)
lastname=names[1];
else
lastname="";
GPA=d;
Id=i;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.