Search code examples
javaarrayssortingbubble-sort

bubble sorting an array of a class


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.

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 <= 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 < (index-1); pass++)
    {
        for (int c = 0; c < (index - 1); c++)
        {
            if  (aStudent[].getName() > 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("===============================");
    }
}

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"

Any help would be greatly appreciated.

thanks

Edit: I used Ashan's suggestion and now it looks like this.

    for (int pass = 0; pass < (index-1); pass++)
    {
        for (int c = 0; c < (index - 1); c++)
        {
            if  (aStudent[c].getName().compareTo(aStudent[c+1].getName()) > 0)
            {
                temp = aStudent[c];
                aStudent[c]=aStudent[+1];
                aStudent[+1]=temp;

That cleared up that error. However, I am now getting a NullPointerException.


Solution

  • You cannot compare strings using operator such as < , > . To compare strings there is a methodd provided in String class called compareTo. This method compares two strings lexicographically.

    compareTo returns

    • 0 incase both the strings are lexicographically equal
    • -1 if the calling string is lexicographically smaller than the input string
    • 1 if the calling string is lexicographically larger than the input stirng

    You can replace the following condition

        if  (aStudent[].getName() > aStudent[c+1].getName())
    

    using compareTo method as:

        if  (aStudent[].getName().compareTo(aStudent[c+1].getName()) > 0)