0

I am passing in the basketball in to the linked bag remove method. I checking the same object that i already passed in. It is returning false.

public static void removeMain(Scanner input,LinkedBag<Basketball> teamX){
        System.out.println("Please enter the team & rank of the object you want to remove.");
        System.out.println("Team?");
        String team = input.nextLine();
        System.out.println("Rank?");
        int rank = input.nextInt();
        input.nextLine();

        if(teamX.remove(new Basketball(team, rank))){
            System.out.println("REMOVED!");
        }else{
            System.out.println("Team was not found due to it not being in the list.");
        }   

    }



public boolean remove(E target){

        boolean found = false;
        int i = 0;
        Node<E> pointer = head;
        Node<E> previous = head;

        while (pointer != null && !found){
            if((pointer.getData()).equals(target)){
                found = true;
            }else{
                pointer = pointer.getLink();
                if(i>0){
                    previous = previous.getLink();
                }
                i++;
            }
        }

        if(found){
            previous.setLink(pointer.getLink());
            numElements--;
        }



        return found;
    }

This is the basketball class

public class Basketball implements Comparable

      public int compareTo(Basketball anotherTeam)
throws ClassCastException
{
    if (!(anotherTeam instanceof Basketball))
        throw new ClassCastException("A Car object expected.");

    if (getRanking() < anotherTeam.getRanking())
        return -1;
    else if (getRanking() > anotherTeam.getRanking())
        return 1;
    else
        return team.compareToIgnoreCase(anotherTeam.getTeam());
}
  • 2
    Please post the code as text, and not as an image – dumbPotato21 Mar 20 '17 at 22:32
  • how do i do that –  Mar 20 '17 at 22:35
  • copy the text, and paste it in the post. – dumbPotato21 Mar 20 '17 at 22:36
  • 1
    Also show the code of your Basketball class (especially the equals method). – dunni Mar 20 '17 at 22:37
  • @CAclao google markdown multi-line code samples. You'll annoy many people otherwise. – Chris Mowforth Mar 20 '17 at 22:38
  • where do you add stuff into the list? How do you know there is anything in the list? do you have a size() method to tell you how big the list is? It will help with debugging to print the i when you're done with i, to see where you get to within your list. – RAZ_Muh_Taz Mar 20 '17 at 22:43
  • Yeah I have a size display and add method. They all work, if i add celtics 1, the display method shows 1 celtics and size shows 1. I thought it was the algorithm of the remove method of the linked bag but it looks good. –  Mar 20 '17 at 22:45
  • Show the equals method of your Basketball class. If you haven't one, you need one as Mikenno mentioned, because you use the equals method when removing and object from the list. – dunni Mar 20 '17 at 22:47
  • Ah i didnt even check my basketball class, made the equals method, worked. Thank you very much for the help! –  Mar 20 '17 at 22:51

2 Answers2

0

have you tried making a costumized ´equals´ ovveride on the BasketBall class ? if not java by default uses the one from "Object" JavaDoc Object which may or may not work depending on how the class was made. for an example try stackoverflow post

EDIT new info

In java true is translated to the value 1(and false 0), thus it can never be equal,either return 0 here or use an equals override instead if there is no need to know a sorted ranking

Community
  • 1
  • 1
Mikenno
  • 294
  • 3
  • 9
0

Rather than defining compareTo(), I think you'd better try to override equals(), because you use point.getData().equals() in your remove() method.

Or you might use your own compareTo() in your remove() method, instead of equlas().

fcmonoid
  • 33
  • 1
  • 6