Reading Keywords from text file
Hello,
I would be grateful to get help with a problem that I have been having. First of all, I have two arrays, and a text file. My program is supposed to read all the keywords in the array, and search for the keyword in the text file, and then outputs all the sentences that contain these keywords.
The 1st array Code:
String[] faults = {
"Misspelled",
"Error",
"Fixed",
"Change",
"Update",
"Out of sync",
"Fail"};
The 2nd array Code:
String[] function = {"Added content assist item", "Added SWT images", "Tidy up build", "Code format","Fixed comments",
"Filters","Polish","Adjust", "Clean up","Minor","Clarify","Removing","Added","Undo fix for bug",};
Step 1: Looks at the the faults array, with all the keywords.
Step 2: Searches for all the keywords in the text file.
Step 3: Prints all the sentences that contains the words in faults.
Step 4: Looks at the function array, with all the keywords.
Step 5: Searches for the all keywords etc.
I have managed to read the text file, but I am stuck on reading keywords from the text file. I have been reading on Hashmap, but my problem is that I don't know how to use the Hashmap with arrays that I have got, or how to use Hashmap to read the keywords from the text file.
I would be very grateful if someone could provide me with any suggestion on how to do what I have mentioned above, or provide me with any starting points that I could use.
Thank you for your time :)
Re: Reading Keywords from text file
First off, welcome!! You show us you code and maybe we can help you out.
Re: Reading Keywords from text file
How are you reading in the text file at the moment?
Have you got it reading in by sentences?
Re: Reading Keywords from text file
Hello, Thank you :)
Currently I am reading the entire file. This is the code that I am using to read the file.
Code:
import java.io.*;
class ReadFile
{
public static void main(String args[])
{
try{
FileInputStream fstream = new FileInputStream("JDT.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String newread;
while ((newread = br.readLine()) != null) {
//System.out.println (newread);
System.out.println(
java.util.Arrays.toString(
newread.split(" ")));
}
in.close();
}catch
(Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}
Edit: All the lines in the text file is one full sentence.
Re: Reading Keywords from text file
Since you seem to be able to use methods on the String class then read each line and see if those words are on that line.
Code:
while read line
for all words in list
if word in line
add to map (or whatever it is you are supposed to d with this stuff)
Re: Reading Keywords from text file
Where would I be adding the arrays?
Or will that be a different method than I just call the array in the method where it would be reading the file?
Re: Reading Keywords from text file
I have no idea where you are getting your arrays from.
I presumed they were constants somewhere.
Re: Reading Keywords from text file
Quote:
Originally Posted by
Tolls
I have no idea where you are getting your arrays from.
I presumed they were constants somewhere.
Yes, but I don't know how I can use the arrays when I am looking for the keywords in the text file. Or is there any other way I can go about this?
I can use Hashmap, by placing the keywords there, but I think that would take a lot of time to actually input all the keywords that I need.
Re: Reading Keywords from text file
You loop over the arrays, looking for each word.
From the requirements in your original post you just need to print out a matching line.
So, for each line, loop over the values in 'faults' and check if the line contains that String.
If so, print it out and break from the inner loop.
I don't actually see any requirement to store the results anywhere.
Re: Reading Keywords from text file
I was trying to use focus on the aspect of printing the sentences out, but is there any way in which I can store the sentences somewhere, e.g. a text file?
Thank you
Re: Reading Keywords from text file
Of course.
You just output to a file instead.
Re: Reading Keywords from text file
To make an For loop, I need a stop condition for the loop stop, and since I don't know how many sentences are there in the text file, how would I stop it? Really confused on this.
Re: Reading Keywords from text file
Why do you need a for loop?
You're already looping through the file using a perfectly good while loop.
Re: Reading Keywords from text file
I was looking at what you have suggested earlier about the for loop.
Quote:
loop over the values in 'faults' and check if the line contains that String.
Would I use an If statement for this?
Re: Reading Keywords from text file
If you've cross-posted this question on another site, it would be considerate of you to tell us and provide links to all cross-posts. Else folks may not be too desiring to help you if it risks wasting their time giving help that's already been given elsewhere.
Re: Reading Keywords from text file
Quote:
Originally Posted by
Fubarable
If you've cross-posted this question on
another site, it would be considerate of you to tell us and provide links to
all cross-posts. Else folks may not be too desiring to help you if it risks wasting their time giving help that's already been given elsewhere.
Sorry, I just posted it there today, after I still couldn't figure it out. But I'll remember to link if next time I ask a question.
Re: Reading Keywords from text file
Quote:
Originally Posted by
Tolls
Why do you need a for loop?
You're already looping through the file using a perfectly good while loop.
I sorted out what I needed to do, but thank you for your time. I really appreciate it, and I learnt some things on what you have said so that helped a lot. :)