Help with this
This is my code for compsci:
// Lab20st.java
// The Student Records Algorithm Program
// Student Version
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
publicclass Lab20st1
{
publicstaticvoid main(String args[])throws IOException
{
List studentArray =new List();
studentArray.getList();
studentArray.checkIfEmpty();
studentArray.pause();
studentArray.display("UNSORTED LIST OF STUDENTS");
studentArray.pause();
studentArray.gpaSort();
studentArray.display("STUDENTS SORTED IN DESCENDING ORDER BY GPA");
studentArray.pause();
Person thisPerson = getPerson();
studentArray.insert(thisPerson);
studentArray.display("STUDENTS SORTED BY GPA WITH NEW STUDENT ADDED");
studentArray.pause();
studentArray.removeFailing();
studentArray.display("STUDENTS SORTED BY GPA WITH FAILING STUDENTS REMOVED");
studentArray.pause();
int index = studentArray.search(thisPerson);
if (index == -1)
System.out.println(thisPerson.name +" is not in the list.\n");
else
System.out.println(thisPerson.name +" is in the list at index "+index+".\n");
studentArray.compareTop3();
studentArray.removeAll();
studentArray.checkIfEmpty();
}
publicstatic Person getPerson()
{
Scanner input =new Scanner(System.in);
System.out.print("\nEnter name: --> ");
String name = input.nextLine();
System.out.print("Enter id:--> ");
int id = input.nextInt();
System.out.print("Enter age:--> ");
int age = input.nextInt();
System.out.print("Enter gpa:--> ");
double gpa = input.nextDouble();
returnnew Person(name,id,age,gpa);
}
}
class Person
{
public String name;
publicint id;
publicint age;
publicdouble gpa;
Person(String n,int ID,int a,double g)
{
name = n;
id = ID;
age = a;
gpa = g;
}
Person(Person clone)
{
name = clone.name;
id = clone.id;
age = clone.age;
gpa = clone.gpa;
}
public String toString()
{
return ("Name: " + name +"\nID#: " + id +"\nAge: " + age +"\nGPA: " + gpa +"\n");
}
publicboolean equals(Person imp)
{
if(age == imp.age && gpa == imp.gpa)
returntrue;
else
returnfalse;
}
}
class List
{
private ArrayList student;// stores array elements
boolean empty;
public List()
{
student =new ArrayList();
}
publicvoid getList()throws IOException
{
// This methos is mostly finished. As it is, it will read in the information from the file,
// but it will not put the information into the ArrayList. You need to add that part.
FileReader inFile =new FileReader("students2.dat");
BufferedReader inStream =new BufferedReader(inFile);
String s1,s2,s3,s4;
int age, id;
double gpa;
while( ((s1 = inStream.readLine()) !=null) &&
((s2 = inStream.readLine()) !=null) &&
((s3 = inStream.readLine()) !=null) &&
((s4 = inStream.readLine()) !=null) )
{
String name = s1;
id = Integer.parseInt(s2);
age = Integer.parseInt(s3);
gpa = Double.parseDouble(s4);
student.add(new Person(name,id,age,gpa));
}
inStream.close();
}
publicvoid display(String listInfo)
{
// This method requires that the toString method of the person class is written
System.out.println("\nDISPLAYING "+ listInfo);
System.out.println();
for (int k = 0; k < student.size(); k++)
System.out.println(student.get(k).toString());
}
publicvoid pause()
{
Scanner input =new Scanner(System.in);
String dummy;
System.out.println("\nPress <Enter> to continue.");
dummy = input.nextLine();
}
privatevoid swap(int x,int y)
{
Person temp =new Person((Person)student.get(x));
student.set(x,student.get(y));
student.set(y,temp);
}
publicvoid gpaSort()
{
// NOTE: This method is provided for you,
//but it will not work if you have not written the swap method
for (int p = 1; p < student.size(); p++)
for (int q = 0; q < student.size()-p; q++)
{
Person thisOne = (Person) student.get(q);
Person nextOne = (Person) student.get(q+1);
if (thisOne.gpa < nextOne.gpa)
swap(q,q+1);
}
}
publicint search(Person thisPerson)
{
int index = -1;
int i = 0;
while(i < student.size() && index == -1)
if(thisPerson.equals(student.get(i)))
index = i;
else
i++;
return index;
}
publicvoid insert(Person newStudent)
{
// precondition: The student list is not empty
boolean in =false;
if(!student.isEmpty())
for(int c = student.size() - 1; c > 0 && !in; c--)
if(newStudent.gpa < ((Person)student.get(c)).gpa)
{
student.add(c+1,newStudent);
in =true;
}
// NOTE: The 110 point version requires that the new student is inserted into
//its proper position in an ArrayList sorted by GPA in decending order.
//All other versions, simply require that the new student is inserted
//at the beginning of the list.
}
publicvoid removeFailing()
{
// HINT: Remember, the list will be sorted in DECENDING order.
boolean end =false;
for(int f = student.size() - 1; f > 0 && !end; f--)
if(((Person)student.get(f)).gpa < 1.9)
student.remove(f);
else
end =true;
}
publicvoid removeAll()
{
student.clear();
}
publicvoid compareTop3()
{
// NOTE: This method is provided for you,
//but it will not work if you have not written the equals method of the Person class.
Person first, second, third;
first = (Person) student.get(0);
second = (Person) student.get(1);
third = (Person) student.get(2);
if (first.equals(second))
System.out.println(first.name+" and "+second.name+" are equal.\n");
if (first.equals(third))
System.out.println(first.name+" and "+third.name+" are equal.\n");
if (third.equals(second))
System.out.println(third.name+" and "+second.name+" are equal.\n");
}
publicvoid checkIfEmpty()
{
if(student.isEmpty())
System.out.println("This list is empty.");
System.out.println("This list is empty.");
}
}
For some reason, the compile errors says it can't find the Scanner class....on some computers, this problem occurs; on others, it doesn't.
Can anyone help me on this? is this something wrong with Java? Thanks.

