So with my code here on the line i bolded im getting the error java.lang.ArrayIn
ID: 3721197 • Letter: S
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 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<n;i++)
{
System.out.println(list[i].toString());
}
}
public static String zipOfYoungest(Student[] list, int n){
Student temp=list[0];
for(int i=0;i<n;i++)
{
for (int j = 1; j < (n - i); j++)
{
if (list[j - 1].getAge() > list[j].getAge())
{
temp = list[j - 1];
list[j - 1] = list[j];
list[j] = temp;
}
}
}
return list[0].getZip();
}
public static Student oldestStudent(Student[] list, int n){
Student temp=list[0];
for(int i=0;i<n;i++)
{
for (int j = 1; j < (n - i); j++)
{
if (list[j - 1].getAge() < list[j].getAge())
{
temp = list[j - 1];
list[j - 1] = list[j];
list[j] = temp;
}
}
}
return list[0];
}
public static int countNewBritain(Student[] list, int n) {
int countNewBritain=0;
for(int i=0;i<n;i++)
{
if(list[i].getZip().equals("06050"))
{
countNewBritain++;
}
}
return countNewBritain;
}
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.
Explanation / Answer
try using
declare the string array with large number so enough memory and check out for error , as its stating array index out of bound
String val = values[1];
hopefuly works out :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.