1

We were given a task to:

  • sort an array list by a dog's tail length (double)
  • if two dogs or more have the same tail length, sort them by name

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;
}
1
  • The compareTo method looks ok (except for the first 3 lines, remove them). You can write some tests to verify that it is working. The problem might be somewhere else, e.g. the if (dogs.get(i).getTailLength() >= tailLength) { looks potentially suspicious. Commented Feb 12, 2019 at 14:39

2 Answers 2

2

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).

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

2 Comments

It is to consume the newline left in the buffer after entering the number for nextDouble(). It is a poor design, Double.parseDouble(scan.nextLine()); should have been used instead, but it is correct.
While your comment is not bad, it is definitely not an answer to OPs question, more a comment. I suggest to remove it, see How to Answer, thanks.
1

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);
                }
            }

        }
    }
}

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.