M Ann
M Ann

Reputation: 107

Sort arraylist by double and String

We were given a task to:

I was able to create a code BUT the correct results do not show up during the first print-out. I have to try again for the sorting to actually happen.

I'm very new to programming and this confuses me. Any ideas? Thanks!

private void listDogs() {
    boolean length = false;
    for (int i = 0; i < dogs.size(); i++) {
        length = true;
    }
    if (dogs.isEmpty()) {
        System.out.println("Error: no dogs in register");

    }else {
        System.out.println("Please enter the tail lenght minimum: ");
        double tailLength = scan.nextDouble();
        scan.nextLine();

        Collections.sort(dogs);
        for (int i = 0; i < dogs.size(); i++) {
            if (dogs.get(i).getTailLength() >= tailLength) {
                System.out.println(dogs.get(i));
                length = true;
            }
        }
        if (length == false) {
                System.out.println("Error: No dog is registered with this tail length.");   
        }       

@Override
public int compareTo(Dog o) {
    // TODO Auto-generated method stub
    int compare = Double.compare(tailLength, o.tailLength);   
    if (compare == 0) {
        compare = Double.compare(tailLength, o.tailLength);    
    }
    if (compare == 0) {
        compare = name.compareTo(o.name);
    }
    return compare;
}

Upvotes: 1

Views: 715

Answers (2)

alexflex25
alexflex25

Reputation: 93

Do you try this?

import java.util.ArrayList;
import java.util.Collections;

public class Dog implements Comparable<Dog> {

    private double tailLength;
    private String name;

    public Dog(final double _tailLength, final String _name) {
        tailLength = _tailLength;
        name = _name;
    }

    @Override
    public String toString() {
        return "Dog [tailLength=" + tailLength + ", name=" + name + "]";
    }

    @Override
    public int compareTo(final Dog o) {
        int res = Double.compare(tailLength, o.getTailLength());
        if (res == 0) {
            res = name.compareTo(o.getName());
        }
        return res;
    }

    public double getTailLength() {
        return tailLength;
    }

    public void setTailLength(final double tailLength) {
        this.tailLength = tailLength;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public static void main(final String[] args) {
        final ArrayList<Dog> dogs = new ArrayList<Dog>();
        dogs.add(new Dog(2, "Dog D"));
        dogs.add(new Dog(2, "Dog A"));
        dogs.add(new Dog(5, "Dog C"));
        dogs.add(new Dog(4, "Dog A"));
        dogs.add(new Dog(3, "Dog A"));
        dogs.add(new Dog(3, "Dog B"));
        dogs.add(new Dog(1, "Dog A"));

        // Sort Dog by tailLength and name
        Collections.sort(dogs);

        final Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("tailLength: ");
            final double inputTaillength = sc.nextDouble();
            for (final Dog dog : dogs) {
                if (dog.tailLength == inputTaillength) {
                    System.out.println(dog);
                }
            }

        }
    }
}

Upvotes: 1

Malte K&#246;lle
Malte K&#246;lle

Reputation: 192

I am not sure if this solves your Problem, but you have a

scan.nextLine();

in your Code and I do not understand if this has to be there, because as far as I know it only waits for you to enter something and then it doesn't even save it in a variable. So you might delete this line (Line 12).

Upvotes: 2

Related Questions