Note that there are some explanatory texts on larger screens.

plurals
  1. PObubble sorting an array of a class
    text
    copied!<p>I wrote a program that is supposed to read in records from a file and enter them into an array of a Student class. I then need to sort them by name.</p> <pre><code>import java.io.*; import java.util.Scanner; public class StudentTest { public static void main(String[] args) { String name; String address; String major; double gpa; int classLevel; int college; String blank; String idNumber; Scanner fileIn = null; try { fileIn = new Scanner (new FileInputStream("student.dat")); } catch (FileNotFoundException e) { System.out.println("File not found"); System.exit(0); } Student[] aStudent = new Student[15]; int index = 0; for (index=0; index &lt;= 15; index++) { while (fileIn.hasNext()) { name = fileIn.nextLine(); address = fileIn.nextLine(); major = fileIn.nextLine(); gpa = fileIn.nextDouble(); classLevel = fileIn.nextInt(); college = fileIn.nextInt(); fileIn.nextLine(); idNumber = fileIn.nextLine(); aStudent[index] = new Student(name, address, major, gpa, classLevel, college, idNumber); aStudent[index].Display(); } } Student temp = null; for (int pass = 0; pass &lt; (index-1); pass++) { for (int c = 0; c &lt; (index - 1); c++) { if (aStudent[].getName() &gt; aStudent[c+1].getName()) { temp = aStudent[]; aStudent[]=aStudent[+1]; aStudent[+1]=temp; } } } } } import java.util.Scanner; public class Student { private String name; private String address; private String major; private double gpa; private int classLevel; private int college; private String idNumber; Scanner keyboard = new Scanner(System.in); public Student(String name, String address, String major, double gpa, int classLevel, int coll, String idNum) { this.name = name; this.address = address; this.gpa = gpa; this.major = major; this.classLevel = classLevel; this.college = coll; this.idNumber = idNum; } public String getName() { return name; } public String getAddress() { return address; } public String getMajor() { return major; } public double getGPA() { return gpa; } public int getClassLevel() { return classLevel; } public int getCollege() { return college; } public String getID() { return idNumber; } public void setAddress(String address) { } public void setMajor(String maj) { } public void setCollege(int coll) { } public void Display() { System.out.println("Name: "+getName()); System.out.println("Address: "+getAddress()); System.out.println("Major: " + getMajor()); System.out.println("GPA: "+getGPA()+" Class Level: "+getClassLevel()+" College: "+getCollege()); System.out.println("ID: "+getID()); System.out.println("==============================="); } } </code></pre> <p>I wrote the sort the way my proffessor described it in class, but I am still getting errors that "the > operator is undefined for the argument type(s) java.laungage.String"</p> <p>Any help would be greatly appreciated.</p> <p>thanks</p> <p>Edit: I used Ashan's suggestion and now it looks like this.</p> <pre><code> for (int pass = 0; pass &lt; (index-1); pass++) { for (int c = 0; c &lt; (index - 1); c++) { if (aStudent[c].getName().compareTo(aStudent[c+1].getName()) &gt; 0) { temp = aStudent[c]; aStudent[c]=aStudent[+1]; aStudent[+1]=temp; </code></pre> <p>That cleared up that error. However, I am now getting a NullPointerException.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload