0

I'm doing a project for class in which you have to make a Hall of Fame and be able to add/remove/search/edit different kinds of bands. Right now, I'm having trouble with searching the index for a specific band because it always returns -1 and I'm not sure why.

Here is my code:

public class HallofFame
{
    public static ArrayList<Band> hallOfFame = new ArrayList<Band>();
    public static Scanner scan = new Scanner(System.in);
    public static void main(String[]args){
        int a = 0;
        while(a == 0){
            System.out.println("What would you like to do?");
            System.out.println("");
            System.out.println("1. Add");
            System.out.println("2. Remove");
            System.out.println("3. Edit");
            System.out.println("4. Clear");
            System.out.println("5. Search");
            System.out.println("6. Quit");
            System.out.println("");
            String choice = scan.nextLine();
            if(choice.equals ("1")){
                add();
            }
            else if(choice.equals ("2")){
                remove();
            }
            else if(choice.equals ("3")){
                edit();
            }
            else if(choice.equals ("4")){
                clear();
            }
            else if(choice.equals ("5")){
                search();
            }
            else if(choice.equals ("6")){
                quit();
                break;
            }
        }
    }

    public static void add(){
        Scanner booblean = new Scanner(System.in);
        System.out.println("What is the name of the band you would like to add?");
        String name = scan.nextLine();
        System.out.println("What kind of genre is this band?");
        String genre = scan.nextLine();
        System.out.println("How many members are in the band?");
        int numMem = scan.nextInt();
        System.out.println("How many songs does this band have?");
        int numSongs = scan.nextInt();
        System.out.println("How many albums does this band have?");
        int numAlbs = scan.nextInt();
        System.out.println("Is this band currently active?");
        String yesno = booblean.nextLine();
        boolean isActive = false;
        if(yesno.equalsIgnoreCase ("yes")){
            isActive = true;
        }
        Band b1 = new Band(name, genre, numMem, numSongs, numAlbs, isActive);
        hallOfFame.add(b1);
        System.out.println("");
        System.out.println("The band " + name + " has been added to the database.");
        System.out.println("");
    }

    public static void remove(){

    }

    public static void edit(){
        System.out.println("What band info do you want to edit?");
        String searchband = scan.nextLine();
    }

    public static void clear(){
        hallOfFame.clear();
    }

    public static void search(){
        System.out.println("What band name are you searching for?");
        String searchband = scan.nextLine();
        int retval = hallOfFame.indexOf(searchband);
        System.out.println("The band " + searchband + " is at index: " + retval);
    }

    public static void quit(){
        System.exit(0);
    }
}

The search method is the one I'm having trouble with.

  • 5
    Please show how you populated `hallOfFame` and example search input that returns `-1`. – rgettman Nov 11 '13 at 23:26
  • Can you show us the whole class please, not just this one method? – Dawood ibn Kareem Nov 11 '13 at 23:28
  • `booblean` - seriously? :D – Birb Nov 11 '13 at 23:49
  • `hallOfFame` is an `ArrayList` so you aren't going to be able to use indexOf with a String. You have to search it yourself. – Radiodef Nov 11 '13 at 23:50
  • A note about your overall execution of the program: the `while(a == 0){` part indicates that you are looping as long as `a` is 0. You should, instead of creating an `int a = 0;`, do something like `boolean running = true;`. Then you go `while(running){` instead of the other while condition. Now, at the 6th choice, the quit choice, instead of calling `quit()` and breaking, simply set `running = false;`. This means that you program will end normally, as opposed to being killed. To compare with human beings, your program will die of old age, rather than being shot at an early age. – Birb Nov 11 '13 at 23:57

4 Answers4

3

The problem is that hallOfFame contains Band objects, but you're searching hallOfFame for a String. Instead, iterate through hallOfFame and compare band names to the inputted string.

musical_coder
  • 3,886
  • 3
  • 15
  • 18
2

Alternatively, you can override the equals method of Band so that indexOf would actually work.

I'd imagine it would read something like this:

@Override

public boolean equals(Object o) {

  return ((Band) o).name==this.name;

}
josephus
  • 8,284
  • 1
  • 37
  • 57
  • I'd suggest returning the following instead: return o instanceof Band && ((Band) o).name==this.name; – JBarberU Nov 12 '13 at 00:32
1

You should override both equals and hashCode to make it work perfectly.

Overriding equals and hashCode in Java

Community
  • 1
  • 1
Tien Nguyen
  • 4,298
  • 9
  • 30
  • 44
1

One way is you need make a Band's constructor with param is name. Then you can search the Object "Band" in ArrayList hallOfFame

Hoa Nguyen
  • 168
  • 1
  • 7