2010-02-20 7 views
2

Ce que je veux faire est de rechercher un HashSet avec un mot-clé ..recherche une collection pour un mot clé

Je 3 classes ...

  1. main()
  2. Bibliothèque
  3. article (CD, DVD, livre des classes)

dans la bibliothèque, je suis en train de faire ma recherche des articles dans les HashSet ..

En classe article est où j'ai la fonction getKeywords ..

ici est la classe ... Articles

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" ; 

    } 

    } 

J'espère que je na pas vous embrouiller? J'ai besoin d'aide avec la itemsForKeyword fonction (mot-clé String) ..

le premier mot-clé étant transmis est « science-fiction » et je veux rechercher les mots-clés dans les jeux et retourner les matchs ..

Qu'est-ce que Je me trompe? Merci

+1

Cela ressemble à un double de http://stackoverflow.com/questions/2293570 –

Répondre

1

Vous devez ajouter quelque chose comme:

String[] keywords = s.getKeywords(); 

for(String word : keywords) { 

    if(word.equals(keyword)) { 

    key.add(s); 
    break; 
    } 
} 

Vous ne pouvez pas comparer une chaîne à un tableau de chaînes. Ce serait mieux si vos mots-clés étaient dans un ensemble. Vous pouvez alors faire juste:

if(s.getKeywords.contains(keyword)) { 
    key.add(s); 
} 
+0

Merci .. j'avais essayé quelque chose de similaire mais ce n'était pas tout à fait raison .. –

0

getKeywords() va retourner un tableau de String, et vous comparez pour voir si ce tableau est égal à une seule chaîne. Cette comparaison retournera toujours faux.

+0

Que puis-je faire pour résoudre ce problème? –

Questions connexes