string searching program.

  • Thread starter elci via JavaKB.com
  • Start date
E

elci via JavaKB.com

i am trying to write a program in java that searches a directory of files
for a string. i need to able to rank the documents in order of relevance.
at the moment i have managed to implement a program that can search a file
for a string. it will then print the line of occurrence, i need to be able
to seach a whole directory with subdirectories, i am having trouble with
this part, can anyone see in my code, below, why i can''t search a folder
of files, i can only search one file.

Code:
private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {

String filepath;
FileDialog fileDialog = new FileDialog(this, "Open...",
FileDialog.LOAD);
fileDialog.show();
if (fileDialog.getFile() == null)
return;

filepath = fileDialog.getDirectory() + File.separator +
fileDialog.getFile();
pathText.setText(filepath);
// TODO add your handling code here:
}

private void closeButtonMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.exit(0);

}

private void pathTextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void searchTextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void searchButtonActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
//String path = "NULL";
int rank = 0;
String text = searchText.getText();
String path = pathText.getText();
System.out.println(path);
boolean blnTitle = false;
boolean blnAuthor = false;
boolean blnAbstxt = false;
String title = "";
String author = "";
String abstxt = "";
int titleStart = text.indexOf(title);
int titleEnd   = titleStart + title.length() - 1;

int abstractStart = text.indexOf(abstxt);
int abstractEnd   = abstractStart + abstxt.length() - 1;

int authorStart = text.indexOf(author);
int authorEnd   = authorStart + author.length() - 1;



try
{
//FileReader fileReader = new FileReader(inFile);
BufferedReader in = new BufferedReader(new FileReader(path));
String line = "";
while ((line = in.readLine())!=null)
{
if (line.indexOf(text)>= 0)
{
resultPane.append(line+"\n");
String delim = "@!#";
StringTokenizer tokenizer = new StringTokenizer
(line,delim,true);

while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
if (token.equals("@"))
{
blnTitle    = true;
blnAbstxt = false;
blnAuthor   = false;
resultPane.append("this was found in the title\
n");
}
if (token.equals("!"))
{
blnTitle    = false;
blnAbstxt   = true;
blnAuthor   = false;
resultPane.append("this was found in the Body\
n");
}
if (token.equals("#"))
{
blnTitle  = false;
blnAbstxt = false;
blnAuthor = true;
resultPane.append("this was found in the Author\
n");
}

if (blnTitle)
title += token;

if (blnAuthor)
author += token;
if (blnAbstxt)
abstxt += token;


}
//System.out.println(line);

rank++;
}

}


if (rank == 0)
resultPane.setText("word not found in file\n");

}
catch(IOException e)
{

System.out.println("error");
}




}

if i try to pass a directory i get an error, can anyone help me with this
please!!! sorry if i am asking a silly question, i am new to java and
unfortunately i am struggling.
 
D

Daniel

I did not look too closely on your code. But from what I see it's not
recursive. Try to make a method like this
public void find(File f){
if(f.isDirectory()){
File[] ff=f.listFiles();
for(int j=0;j<ff.length;j++){
find(ff[j];

}
}
}
of coruse you need the code to do the actual string comparision and
also you must handle the returnvalue in a good way, so you know if it
was found or not. But this should get you started.
/Daniel
i am trying to write a program in java that searches a directory of files
for a string. i need to able to rank the documents in order of relevance.
at the moment i have managed to implement a program that can search a file
for a string. it will then print the line of occurrence, i need to be able
to seach a whole directory with subdirectories, i am having trouble with
this part, can anyone see in my code, below, why i can''t search a folder
of files, i can only search one file.

Code:
private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {

String filepath;
FileDialog fileDialog = new FileDialog(this, "Open...",
FileDialog.LOAD);
fileDialog.show();
if (fileDialog.getFile() == null)
return;

filepath = fileDialog.getDirectory() + File.separator +
fileDialog.getFile();
pathText.setText(filepath);
// TODO add your handling code here:
}

private void closeButtonMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.exit(0);

}

private void pathTextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void searchTextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void searchButtonActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
//String path = "NULL";
int rank = 0;
String text = searchText.getText();
String path = pathText.getText();
System.out.println(path);
boolean blnTitle = false;
boolean blnAuthor = false;
boolean blnAbstxt = false;
String title = "";
String author = "";
String abstxt = "";
int titleStart = text.indexOf(title);
int titleEnd   = titleStart + title.length() - 1;

int abstractStart = text.indexOf(abstxt);
int abstractEnd   = abstractStart + abstxt.length() - 1;

int authorStart = text.indexOf(author);
int authorEnd   = authorStart + author.length() - 1;



try
{
//FileReader fileReader = new FileReader(inFile);
BufferedReader in = new BufferedReader(new FileReader(path));
String line = "";
while ((line = in.readLine())!=null)
{
if (line.indexOf(text)>= 0)
{
resultPane.append(line+"\n");
String delim = "@!#";
StringTokenizer tokenizer = new StringTokenizer
(line,delim,true);

while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
if (token.equals("@"))
{
blnTitle    = true;
blnAbstxt = false;
blnAuthor   = false;
resultPane.append("this was found in the title\
n");
}
if (token.equals("!"))
{
blnTitle    = false;
blnAbstxt   = true;
blnAuthor   = false;
resultPane.append("this was found in the Body\
n");
}
if (token.equals("#"))
{
blnTitle  = false;
blnAbstxt = false;
blnAuthor = true;
resultPane.append("this was found in the Author\
n");
}

if (blnTitle)
title += token;

if (blnAuthor)
author += token;
if (blnAbstxt)
abstxt += token;


}
//System.out.println(line);

rank++;
}

}


if (rank == 0)
resultPane.setText("word not found in file\n");

}
catch(IOException e)
{

System.out.println("error");
}




}

if i try to pass a directory i get an error, can anyone help me with this
please!!! sorry if i am asking a silly question, i am new to java and
unfortunately i am struggling.
 
I

iamfractal

elci via JavaKB.com said:
i am trying to write a program in java that searches a directory of files
for a string. i need to able to rank the documents in order of relevance.
at the moment i have managed to implement a program that can search a file
for a string. it will then print the line of occurrence, i need to be able
to seach a whole directory with subdirectories, i am having trouble with
this part, can anyone see in my code, below, why i can''t search a folder
of files, i can only search one file.

Code:
private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {

String filepath;
FileDialog fileDialog = new FileDialog(this, "Open...",
FileDialog.LOAD);
fileDialog.show();
if (fileDialog.getFile() == null)
return;

filepath = fileDialog.getDirectory() + File.separator +
fileDialog.getFile();
pathText.setText(filepath);
// TODO add your handling code here:
}

private void closeButtonMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.exit(0);

}

private void pathTextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void searchTextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void searchButtonActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
//String path = "NULL";
int rank = 0;
String text = searchText.getText();
String path = pathText.getText();
System.out.println(path);
boolean blnTitle = false;
boolean blnAuthor = false;
boolean blnAbstxt = false;
String title = "";
String author = "";
String abstxt = "";
int titleStart = text.indexOf(title);
int titleEnd   = titleStart + title.length() - 1;

int abstractStart = text.indexOf(abstxt);
int abstractEnd   = abstractStart + abstxt.length() - 1;

int authorStart = text.indexOf(author);
int authorEnd   = authorStart + author.length() - 1;



try
{
//FileReader fileReader = new FileReader(inFile);
BufferedReader in = new BufferedReader(new FileReader(path));
String line = "";
while ((line = in.readLine())!=null)
{
if (line.indexOf(text)>= 0)
{
resultPane.append(line+"\n");
String delim = "@!#";
StringTokenizer tokenizer = new StringTokenizer
(line,delim,true);

while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
if (token.equals("@"))
{
blnTitle    = true;
blnAbstxt = false;
blnAuthor   = false;
resultPane.append("this was found in the title\
n");
}
if (token.equals("!"))
{
blnTitle    = false;
blnAbstxt   = true;
blnAuthor   = false;
resultPane.append("this was found in the Body\
n");
}
if (token.equals("#"))
{
blnTitle  = false;
blnAbstxt = false;
blnAuthor = true;
resultPane.append("this was found in the Author\
n");
}

if (blnTitle)
title += token;

if (blnAuthor)
author += token;
if (blnAbstxt)
abstxt += token;


}
//System.out.println(line);

rank++;
}

}


if (rank == 0)
resultPane.setText("word not found in file\n");

}
catch(IOException e)
{

System.out.println("error");
}




}

if i try to pass a directory i get an error, can anyone help me with this
please!!! sorry if i am asking a silly question, i am new to java and
unfortunately i am struggling.


Hi!

Recursive file listing is a standard problem; you may learn loads from
doing it yourself, but be prepared for the hair-pulling.

Alternately, have a look at a standard solution:
http://www.javapractices.com/Topic68.cjp

Hope it helps.


..ed

www.EdmundKirwan.com - Home of The Fractal Class Composition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top