Results 1 to 17 of 17
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By Tolls

Thread: Reading Keywords from text file

  1. #1
    VrutiD is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Smile 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
    Java Code:
    String[] faults = {
    				"Misspelled",
    				"Error",
    				"Fixed",
    				"Change",
    				"Update",
    				"Out of sync",
    				"Fail"};
    The 2nd array
    Java 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 :)

  2. #2
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default Re: Reading Keywords from text file

    First off, welcome!! You show us you code and maybe we can help you out.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default 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?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    VrutiD is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default 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.

    Java 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.
    Last edited by VrutiD; 02-19-2013 at 03:17 PM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default 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.
    Java 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)
    VrutiD likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    VrutiD is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default 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?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default Re: Reading Keywords from text file

    I have no idea where you are getting your arrays from.
    I presumed they were constants somewhere.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    VrutiD is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: Reading Keywords from text file

    Quote Originally Posted by Tolls View Post
    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.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    VrutiD is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default 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

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default Re: Reading Keywords from text file

    Of course.
    You just output to a file instead.
    VrutiD likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    VrutiD is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default 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.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    VrutiD is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: Reading Keywords from text file

    I was looking at what you have suggested earlier about the for loop.

    loop over the values in 'faults' and check if the line contains that String.
    Would I use an If statement for this?

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  16. #16
    VrutiD is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: Reading Keywords from text file

    Quote Originally Posted by Fubarable View Post
    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.
    Last edited by VrutiD; 02-20-2013 at 09:53 PM.

  17. #17
    VrutiD is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: Reading Keywords from text file

    Quote Originally Posted by Tolls View Post
    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. :)

Similar Threads

  1. Reading a text file
    By dougie1809 in forum New To Java
    Replies: 23
    Last Post: 03-12-2012, 11:22 PM
  2. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  3. Replies: 0
    Last Post: 02-25-2011, 08:13 AM
  4. Help reading a text file.
    By Kristoba in forum New To Java
    Replies: 5
    Last Post: 03-11-2010, 08:42 AM
  5. Reading a text file
    By diegosened in forum New To Java
    Replies: 4
    Last Post: 01-15-2010, 11:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •