Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: String Matcher finding only char not a whole string

  1. #1
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default String Matcher finding only char not a whole string

    I have this working right and it does everything to a degree but when I search a file for a string that is gathered from user input it does not do it correctly. If you just type in one letter or number EX: 1 or d it finds the match if any and prints out the line that the match occurs in just fine and dandy. If there is no matches it breaks the loop and says no matches and that fine too. Am i missing something here when dealing with strings and the pattern matcher in java? Thanks for any help
    public static void search()
       {
          LineNumberReader lineReader = null;
          try {
                String sStr;
                System.out.println("Enter your search term");
                Scanner sScan = new Scanner(System.in);
                sStr = sScan.nextLine();
                Pattern pat = Pattern.compile(sStr, Pattern.CASE_INSENSITIVE);
     
                System.out.println("Your searching for " + pat);
                lineReader = new LineNumberReader( new FileReader("Book-text.dat"));
                String line = null;
     
                while ((line = lineReader.readLine()) != null)
                {
                    Matcher matcher = pat.matcher(line);
     
                    if (matcher.find())
                    {
     
                        String msg = "Found Match "  + line;
                        System.out.println(msg);
     
                    }else{System.out.println("No Matches"); break;}
                    matcher.reset();
                }
     
        }
        catch (FileNotFoundException ex) {
          ex.printStackTrace();
        }
        catch (IOException ex){
          ex.printStackTrace();
        }
        finally {
          try {
            if (lineReader!= null) lineReader.close();
          }
          catch (IOException ex) {
            ex.printStackTrace();
          }
        }
     
     
     
     
       }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String Matcher finding only char not a whole string

    it does not do it correctly
    Please define a user input and line you would expect to match which doesn't...or better yet, post an SSCCE where the problem is clearly demonstrated. Nothing looks conspicuous in your code

  3. #3
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: String Matcher finding only char not a whole string

    This is the output of the code
    Found Match Book TITLE: How to Read: Author Gus: Number of PAges 12: Price 12.00;
    The input from the user is any string say "Gus"
    What should happen is any line in the text file should be pulled up that have gus in it anywhere.
    When I type in Gus it says no matchs, but when I type in g it give the match that has any g in the line including lines with Gus even though it says that there are no matches for the string gus.
    Is the compiler just messing with me?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String Matcher finding only char not a whole string

    Not sure I understand. You say "this is the output"...do you mean this is the expected output?

    SSCCE's are wonderful things...take this for example. Took less than a minute and shows what you are telling us would work.
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test{
    	public static void main(String[] args){
    		String text = "TITLE: How to Read: Author Gus: Number of PAges 12: Price 12.00;";
    		String regex = "Gus";
    		Pattern patt = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    		Matcher matcher = patt.matcher(text);
    		if ( matcher.find() ){
    			System.out.println("Found");
    		}else{
    			System.out.println("Not Found");
    		}
    	}
    }
    Output: Found
    Given this works and there are no glaring things in your posted code above, I recommend boiling your code to an SSCCE that we can run and demonstrates the issue more clearly.

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: String Matcher finding only char not a whole string

    I'm quite sure I know what's going on to a certain extent.

    At this stage I'm going to assume the line with gus is not on the first line in the .dat file.
    When you read your first line, you check whether that line is a match. If it isn't you go into the else clause
    and break, therefore you end there and won't find anything further on.

    To fix this, simply remove the break; from the else clause. You'll most likely get the no match found for every line until you reach gus though, so you might want to sort that out.
    Last edited by newbie; February 16th, 2011 at 03:53 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. The Following 2 Users Say Thank You to newbie For This Useful Post:

    copeg (February 16th, 2011), Kakashi (February 18th, 2011)

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String Matcher finding only char not a whole string

    Doh...kudos to newbie for pointing that out (serves me right for glancing over the code so quickly)

  8. The Following User Says Thank You to copeg For This Useful Post:

    Kakashi (February 18th, 2011)

  9. #7
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: String Matcher finding only char not a whole string

    Actually the break; works fine and does what I want it to and break when there are no matches.
    I finally got what you ment by the SSCCE and here it is: and with the attached Book-Text.txt file it works fine but still not the wanted output.
    Output should be this
    B o o k T i t l e : H o w t o R e a d , A u t h o r : G u s t h e 3 , N u m b e r o f P a g e s : 4 5 0 0 , P r i c e : 5 0 0 0 . 1 3 ;
    B o o k T i t l e : G u s , A u t h o r : G G G G G , N u m b e r o f P a g e s : 1 2 0 0 , P r i c e : 1 2 3 . 0 ;
    when the input from the user is gus
    it gives this when you type in the letter g but not when you type in the string gus. It gives no match when you type in gus as the search string.
    import java.io.*;
    import java.util.Scanner;
    import java.util.regex.*;
     
    public class SearchTest {
        public static void main(String args[])
        {
            LineNumberReader lineReader = null;
          try {
                String sStr;
                System.out.println("Enter your search term");
                Scanner sScan = new Scanner(System.in);
                sStr = sScan.nextLine();
                Pattern pat = Pattern.compile(sStr, Pattern.CASE_INSENSITIVE);
     
                System.out.println("Your searching for " + pat);
                lineReader = new LineNumberReader( new FileReader("Book-text.txt"));
                String line = null;
     
                while ((line = lineReader.readLine()) != null)
                {
                    Matcher matcher = pat.matcher(line);
     
                    if (matcher.find())
                    {
     
                        String msg = "Found Match "  + line;
                        System.out.println(msg);
     
                    }else{System.out.println("No Matches"); break;}
                    matcher.reset();
                }
     
        }
        catch (FileNotFoundException ex) {
          ex.printStackTrace();
        }
        catch (IOException ex){
          ex.printStackTrace();
        }
        finally {
          try {
            if (lineReader!= null) lineReader.close();
          }
          catch (IOException ex) {
            ex.printStackTrace();
          }
        }
     
     
        }
     
    }
    Attached Files Attached Files

  10. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: String Matcher finding only char not a whole string

    I'd still argue I was kinda correct, but who knows
    If I am correct, then when you type in "g" it matches say..the g in pages of the first line and therefore re-enters the loop on the next line and finds the G in gus.

    If you leave the break in, and type gus, it won't find it on the first line and it will exit.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  11. The Following User Says Thank You to newbie For This Useful Post:

    Kakashi (February 18th, 2011)

  12. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String Matcher finding only char not a whole string

    Actually the break; works fine and does what I want it to and break when there are no matches
    Newbie is correct....sure the break works, breaks in the sense that it prevents reading the rest of the file if a hit is not found on the first line of said file. Enter in a regex that is not found in the first line of the file and it will stop searching. As newbie pointed out, remove that break

  13. The Following User Says Thank You to copeg For This Useful Post:

    Kakashi (February 18th, 2011)

  14. #10
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: String Matcher finding only char not a whole string

    The thing is if I take out the break it just print "no match found" 3 times when I type in Gus which I made sure is in the file.

  15. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String Matcher finding only char not a whole string

    Quote Originally Posted by Kakashi View Post
    The thing is if I take out the break it just print "no match found" 3 times when I type in Gus which I made sure is in the file.
    Then why not remove that line so it doesn't do that? If you want to know if the value was found, count how many times it is found...then at the end of reading of that count is zero print out that no match was found.

  16. #12
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: String Matcher finding only char not a whole string

    Thanks for all the help here I got it working now. The file had spaces in it which I did not realize so I use an object stream instead of the randomaccess file to write it and then started to look for an object to match it and that worked fine.

Similar Threads

  1. Char to String - Gender method.
    By ibby50 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 2nd, 2012, 12:42 PM
  2. Finding frequency and probability of characters in a string
    By Aberforth in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 02:02 AM
  3. finding String in a file
    By nasi in forum Java Theory & Questions
    Replies: 12
    Last Post: May 31st, 2010, 01:16 PM
  4. Convert CHAR to STRING
    By fh84 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 29th, 2009, 09:21 PM
  5. Need help with concatenizing char? to string?
    By bh-chobo in forum Java SE APIs
    Replies: 3
    Last Post: October 16th, 2009, 08:40 AM