Question
So with my code here on the line i bolded im getting the error java.lang.ArrayIndexOutOfBoundsException: 1 at Tester.main(Tester.java:29) But if I change it set to
So with my code here on the line i bolded im getting the error
java.lang.ArrayIndexOutOfBoundsException: 1
at Tester.main(Tester.java:29)
But if I change it set to 1 the error moves to the next line
My first class is:
public class Student { private String name; private int age; private double gpa; private String zip;
public Student(String name, int age, double gpa, String zip) {
this.name = name;
this.age = age;
this.gpa = gpa;
this.zip = zip;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public double getGpa() { return gpa; }
public void setGpa(double gpa) { this.gpa = gpa; }
public String getZip() { return zip; }
public void setZip(String zip) { this.zip = zip; }
@Override public String toString() { return "Name: "+name+" Age: "+age+" GPA: "+gpa+" Zip: "+zip; } }
-----------------Then the second class is this, and this is where the error is--------------------------
import java.io.*; import java.util.*; public class Tester { public static void main(String[] args) throws IOException { Student[] students=new Student[20];
File file = new File("M:\\CS151\\Student\\inData.txt");
Scanner input = new Scanner(file);
int i=0;
int n=7;
while(input.hasNextLine()) { String line=input.nextLine(); String[] values=line.split("\\s"); String name=values[0]; int age = Integer.parseInt(values[1]); double gpa = Double.parseDouble(values[2]); String zip=values[3]; Student student=new Student(name, age, gpa, zip); students[i]=student; i++; }
Student[] originalOrder=Arrays.copyOf(students, n);
printList(students, n);
System.out.println("Zip of Youngest: "+zipOfYoungest(students, n));
System.out.println("Oldest Student Details: "+oldestStudent(students,n));
System.out.println("Count New Britain: "+countNewBritain(students,n));
printListReversed(originalOrder,n);
input.close();
}
public static void printList(Student[] list, int n) { System.out.println(n +" Student Details: "); for(int i=0;i public static String zipOfYoungest(Student[] list, int n){ Student temp=list[0]; for(int i=0;i } } return list[0].getZip(); } public static Student oldestStudent(Student[] list, int n){ Student temp=list[0]; for(int i=0;i public static int countNewBritain(Student[] list, int n) { int countNewBritain=0; for(int i=0;i public static void printListReversed(Student[] list, int n) { System.out.println("Students in reverse order:"); for(int i=n-1;i>=0;i--) { System.out.println(list[i].toString()); } } } So yeah it will move the same error to the next line if i just set the int = 1 instead of the phraseint, any help is great thanks.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started