tags:

views:

56

answers:

2

What i want to do is search a hashset with a keyword..

I have 3 classes...

  1. main()
  2. Library
  3. Item(CD, DVD,Book classes)

In library i am trying to do my search of the items in the hashsets..

In Item class is where i have the getKeywords function..

here is the Items class...

import java.io.PrintStream;
import java.util.Collection;
import java.util.*;


class Item
{

private String title;
private String [] keywords;


public String toString()
{

String line1 = "title:    " + title + "\n" + "keywords: " + Arrays.toString(keywords);
return line1;
}


public void print()
{

System.out.println(toString());

}


public Item()
{

}

public Item(String theTitle, String... theKeyword)
{

this.title = theTitle;
this.keywords = theKeyword;

}

public String getTitle()
{
return title;
}

public String [] getKeywords()
{

    return keywords;

}


}

class CD extends Item
{

private String artist;
private String [] members;
// private String [] keywords;
private int number;


public CD(String theTitle, String theBand, int Snumber, String... keywords)
{
    super(theTitle, keywords);
    this.artist = theBand;
    this.number = Snumber;
   // this.keywords = keywords;

}


  public void addband(String... member)
{
    this.members = member;

}

public String getArtist()
{

    return artist;

}

public String [] getMembers()
{
 return members;   
 }


 // public String [] getKeywords()
 // {

  //  return keywords;

 //}


 public String toString()
 {



   return  "-Music-" + "\n"
     + "band:     " + artist + "\n" 
     + "# songs:  " + number + "\n" 
     + "members:  " + Arrays.toString(members) 
     + "\n" + super.toString() 
    // + "keywords: " + Arrays.toString(keywords) 
     + "\n" + "\n" ;

 }


 public void print()
 {


    System.out.println(toString());

 }


 }


 class DVD extends Item
{

private String director;
private String [] cast;
private int scenes;
 // private String [] keywords;


public DVD(String theTitle, String theDirector, int nScenes, String... keywords)
{
    super(theTitle, keywords);
    this.director = theDirector;
    this.scenes = nScenes;
   // this.keywords = keywords;

}

public void addmoviecast(String... members)
{
    this.cast = members;


}

public String [] getCast()
{
    return cast;

}



public String getDirector()
{
    return director;
}

 // public String [] getKeywords()
 // {

  //  return keywords;

 // }




 public String toString()
 {

    return "-Movie-" + "\n"
     + "director: " + director + "\n"
     + "# scenes: " + scenes + "\n"
     + "cast:     " + Arrays.toString(cast) + "\n"
     + super.toString() 
    // + "keywords: " + Arrays.toString(keywords) 
     + "\n" + "\n" ;

}

public void print()
{

  System.out.println(toString());  

}


}


class Book extends Item
{

private String author;
private int pages;


public Book(String theTitle, String theAuthor, int  nPages, String... keywords)
{
    super(theTitle, keywords);
    this.author = theAuthor;
    this.pages = nPages;
   // this.keywords = keywords;

}


public String getAuthor()
{

    return author;

}

//public String [] getKeywords()
// {

 //   return keywords;

//}




 public void print()
{

    System.out.println(toString());  

}


 public String toString()
{

    return "-Book-" + "\n"
     + "Author:   " + author + "\n" 
     + "# pages   " + pages + "\n"
     + super.toString() 
    // + "keywords: " + Arrays.toString(keywords) 
     + "\n" + "\n" ;

  }

  }

I hope i didnt confuse you? I need help with the itemsForKeyword(String keyword) function..

the first keyword being passed in is "science fiction" and i want to search the keywords in the sets and return the matches..

What am i doing so wrong? Thank you

A: 

getKeywords() is going to return an array of String, and you are comparing to see if that array is equal to a single String. That comparison will always return false.

Carl Manaster
what can i do to fix this?
icelated
+1  A: 

You need to add something like:

String[] keywords = s.getKeywords();

for( String word : keywords ) {

  if( word.equals( keyword ) ) {

    key.add(s);
    break;
  }
}

You can't compare a string to a string array. It would be better if your keywords were in a Set. You could then just do:

if( s.getKeywords.contains( keyword ) ) {
  key.add(s);
}
Binary Nerd
Thank you.. i had tried something similar but it wasnt quite right..
icelated