1

I am stuck on this following piece of code I have been trying to use a bubble sort to sort the array of objects by surname then print it out. where Am i going wrong and how can i print it out? here is my code

for(int i = 1; i < clipArray.length; i++) {
    for(int j = 0; j < clipArray.length; j++) {
        if(((clipArray[j].getSurname()).compareToIgnoreCase((clipArray[j+1].getSurname()))) {
            Clip temp = clipArray[j];
            clipArray[j] = clipArray[j+1];
            clipArray[j+1] = temp;
        }
    }
}

problem: required boolean, found int

9
  • What is the issue you are facing in printing the array? Commented Oct 24, 2013 at 14:56
  • 1
    First problem I see is that clipArray[j+1] will index out of the array (see your second loop) Commented Oct 24, 2013 at 14:56
  • 1
    Tell us what's going wrong first. Provide some input and output. SO is not for code dumps. Please describe your problem, steps you've taken to try solving it, etc. Commented Oct 24, 2013 at 14:56
  • how can I fix this sort? Commented Oct 24, 2013 at 14:57
  • 1
    That's because compareToIgnoreCase returns an int. You need to compare this result with another integer like < 0. Commented Oct 24, 2013 at 14:58

4 Answers 4

2

The problem is that String#compareToIgnoreCase returns an int and you require a boolean result for if statement. From its javadoc:

Returns

a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations

So, compare the result of this method with another number according to the definition.

if (clipArray[j].getSurname().compareToIgnoreCase(clipArray[j+1].getSurname()) < 0) {
    //...
}

Also, you have another error in your for loop declarations. They should be like this:

for(int i = 1; i < clipArray.length; i++) {
    for(int j = 0; j < clipArray.length - 1; j++) { //spot the difference
        //...
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much i understand now! last problem i'm having is that I can't print the sorted array
System.out.println(clipArray[].getSurname());
After you are done sorting the array, create a new loop and print out each surname.
2

Your Comparator class

class SampleComparator implements Comparator<Clip> {
    @Override
    public int compare(Clip o1, Clip o2) {

           return o1.getSurname().compareToIgnoreCase(o2.getSurname());
   }
}

Your sorting here

Arrays.sort(clipArray,new SampleComparator());

2 Comments

-1: This is a homework exercise. Using Arrays#sort is obviously not an answer.
This isn't the issue. Why are people up-voting this?
2

The error is because

compareToIgnoreCase return int values not boolean.

returns a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.

If only takes boolean.

1 Comment

so how can i fix it so that it is comparing the strings and i can then sort it alphabetically?
0

I assume your clipArray is filled with objects that support the getSurname()-method.

The method compareToIgnoreCase returns an int, so you should compare it with a value in your if-statement, like

if(foo.compareTo(bar) > 0)

Comments

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.