PDA

View Full Version : Problemi con la ricerca nel testo


ZanTeo
30-11-2011, 21:46
Salve, il metodo che devo sviluppare deve cercare una determinata frase in un testo, usando lo StringTokenizer ho sviluppato il metodo:
//il metodo verifica se la frase è presente nel testo
public boolean cercaFrase(String testo,String frase){
StringTokenizer parolaTesto=new StringTokenizer(testo);
StringTokenizer parolaFrase=new StringTokenizer(frase);
boolean stato=false;
String pt=parolaTesto.nextToken();
String pf=parolaFrase.nextToken();
String inizio=pf;
while(!stato||!parolaTesto.hasMoreTokens()){
if(pt==pf){
pt=parolaTesto.nextToken();
pf=parolaFrase.nextToken();
if(!parolaFrase.hasMoreTokens())
stato=true;
}
pt=parolaTesto.nextToken();
//mi serve un codice che mi porti parolaFrase al token iniziale
}
return stato;
}
oltre a mancarmi la parte descritta nel commento il codice mi genera un errore, dove sbaglio? è giusto il metodo che ho usato? ci sono metodi/strumenti migliori?

ZanTeo
30-11-2011, 21:58
Uso il main
public static void main(String[] args) throws IOException {

CercaNelTesto cnt=new CercaNelTesto();
FileInputStream fis=new FileInputStream("Lettore a_rmato.pdf");
File f=new File("Lettore a_rmato.pdf");
EstrattoreTesto et=new EstrattoreTesto(fis,f.getName());
System.out.println("Il file si chiama: "+et.getNome());
String frase="il lettore";
System.out.println("Cerco la frase: "+frase);
int n=0;
boolean fine =true;
while(!fine){
n++;
System.out.println("Controllo la pagina: "+n);
if(cnt.cercaFrase(et.getTestoPagina(n), frase))
System.out.println("La frase è nella pagina: "+n);
else
System.out.println("La frase non è nella pagina: "+n);
if(n==et.getNumeroPg())
fine=false;
}
}
che stampa
Il file si chiama: Lettore a_rmato.pdf
Cerco la frase: il lettore