Results 1 to 3 of 3
  1. #1
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Scanning document and finding certain criteria.

    Currently have an assignment where we show an open prompt, have the user select a text file, prompt for a word to search for, then display a dialog saying how many times that word was found among other things. One of the things to search for are the words that come closest before and after the target word. So, for example, if the target is the word "the", the alphabetically previous and next words might be "that" and "there".

    So heres the start of the code for that portion. I have everything else working, just gotta figure out how to find these words..
    Java Code:
    ...
    try
    		{
    			String charequals = "";                    //hastily named variable to keep track of common words...
    			Scanner scanner = new Scanner(text);  //text is the File method name to keep ahold of the text file
    			while (scanner.hasNext())
    			{
    				
    				word = scanner.next().toLowerCase();
    				while(scanner.next().toLowerCase().charAt(0)==userTarget.charAt(0))   //userTarget is the users typed in word. 
    				{
    				//This is where I'm stuck. not sure how to keep comparing to find the right word. Maybe an easier method is available?	
    				}
    EDIT***
    A friend figured out how to get it to find the previous word, but the next word part wont work using a similar code...
    Java Code:
    if (word.compareTo(userTarget) < 0 && word.compareTo(prev) > 0)       //this works fine for the word alphabetically previous
    	            {
    	                   prev = word;                  
    	            }
    	            if(word.compareTo(userTarget) > 0 && word.compareTo(next) < 0) //reversing the signs, this does NOT work... why?
    	                   next = word;                  
    	            
    				if(word.equals(userTarget))
    				{
    					targetTimes++;
    				}
    Last edited by falkon114; 01-27-2011 at 03:34 AM.

  2. #2
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    I think the scanner returns a String containing the next line of text in the file.
    You would need to split the line into words.
    some_word.equalsIgnoreCase(userTarget) would then compare the words.

  3. #3
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    it works fine for what I have already. scanner.next() looks for items separated by whitespace. My code already works for finding how many times "the" is present in the document, as well as how many characters the document contains, so it's not just lines.

Similar Threads

  1. hibernate criteria with relations
    By GlWy in forum Advanced Java
    Replies: 1
    Last Post: 01-18-2011, 09:58 AM
  2. Replies: 1
    Last Post: 02-05-2010, 11:41 AM
  3. Hibernate Criteria
    By stunaz in forum JDBC
    Replies: 0
    Last Post: 12-02-2008, 04:20 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
  •