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
Code :
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();
}
}
}
Re: String Matcher finding only char not a whole string
Quote:
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
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?:-?
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.
Code :
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.
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.
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)
1 Attachment(s)
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.
Code :
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();
}
}
}
}
Re: String Matcher finding only char not a whole string
I'd still argue I was kinda correct, but who knows :P
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.
Re: String Matcher finding only char not a whole string
Quote:
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
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.
Re: String Matcher finding only char not a whole string
Quote:
Originally Posted by
Kakashi
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.
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.