0

I'm attempting to create a method to bubblesort an array of objects to be displayed to the user in alphabetical order based on the name field assigned to the object. I've been playing around with trying to figure this out for a few days now and for the life of me I can't do it. All the research I've done hasn't really helped, tried a bunch of different code and they all seem to just stop the program/give me more issues. So I figured I'd ask. This is what I'm working with now.

public static void sortName(Candidate candidate[])
  {
    int j;
    boolean finished = true; 
    String temp;
    while ( finished )
    {
        finished = false;
        for (j=0;j<candidate.length-1;j++)
        {
             if (candidate[j].getName().compareTo(candidate[j+1].getName())>0)
                {                                            
                   temp = candidate[j].getName();
                   candidate[j].getName() = candidate[j+1].getName();
                   candidate[j+1].getName() = temp; 
                   finished = true;
                       } 
               } 
        } 
  } 

The lines 2nd/3rd lines in the if statement are giving me errors saying it's expecting a variable but finding a value. I suspect it's something to do with trying to access the private name field in the Candidate class through .getName(), but I'm unsure of how else to do this. Any help?

EDIT: I'm now getting an NPE error on the following line

if (candidate[j].compareTo(candidate[j+1])>0)

Here is my compareTo method from my Candidate class.

public int compareTo(Candidate candidate)
{
    int candidateCompare = this.getName().compareTo(candidate.getName());         
    if (candidateCompare != 0)
        return candidateCompare;
    else{
        if (this.getName().compareTo(candidate.getName()) < 0)
           return -1;
        if (this.getName().compareTo(candidate.getName()) > 0)
            return 1;
    }
    return 0;
}

I do not understand why this is happening, tried researching a solution but found no help.

EDIT 2: I have the following lines in my do-while loop where I get user input for the data fields of my class. In this loop, the a new object with the data is created, and assigned to my object array named origCandidateList. Here is the code.

 origCandidateList[candidateCount] = new Candidate(candidateName, candidateAge, candidateParty, candidateIssue);
 candidateCount++;

I initially had the first line below the candidateCount increment, so the array was starting at 1 instead of 0, but this did not fix the issue. I still get an NPE error. The array prints out fine, so the data is going into it. But something seems to be going awry with my compareTo method.

1
  • 2
    and BTW boolean finished = false; while (!finished) is much more readable... Commented Sep 15, 2015 at 18:30

3 Answers 3

2

You can't assign values to candidate[j].getName(). There are no "L-Values" like in C++, where you can do stuff like that (e.g. myVector.at(j) = newValue;).

You must use something like candidate[j].SetName(candidate[j+1].getName());

Better yet, switch out the whole object by doing candidate[j] = candidate[j+1].

Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense, I guess switching only the name fields of the object would cause more problems anyway. Thank you.
0

Not to be disrespectful, but is there a reason you want to do a bubble sort instead of using the built-in sort mechanism? The bubble sort is horribly inefficient for anything but a relatively small dataset.

Regardless, you need to remember that Java (generally) works with objects, so you should swap the objects in the array rather than trying to switch an object member.

1 Comment

It's for a project, required we use a bubble sort.
0

Try saving your variables in a String before assigning new values to them.

String temp = canditate[j].getName();
String firstCandName = candidate[j].getName();
String secondCandName = canditate[j+1].getName();
firstCandName = secondCandName;
secondCandName = temp;

The reason why for example candidate[j].getName() = temp, doesn't work is because candidate[j].getName() is not a variable, and the left side of the argument should be a variable, therefore this will give you an error. Try doing what I did above it should work fine for you. Just to clarify what I mean by candiate[j].getName() is not a variable. getName() method returns a String therefore this whole function is a String. So when you put it on the left side of an equality and try to modify it, Java wouldn't know what to modify. It is like saying

"Sample String" = "Another Sample String" instead of,

String sampleString = "Sample String";
sampleString = "Another Sample String";

Although this should work for you, I personally think you are better off swapping the objects inside the array.

8 Comments

What's the point of swapping the names in local variables only? It won't have any permanent effect and the algorithm therefore won't sort anything at all!
Thank you for your response, can you view my edit? I'm still caught up in this and completed stumped on where to go.
You're absolutely right @Waggili I got so focused on the error that I forgot about the sorting. My bad. setName would then work fine but as I mentioned switching objects is the best solution to go for.
@Paul I suppose there is something wrong with the array, double check your array and let me know.
@iskandarchacra I added another edit with what's happening.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.