473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Looking for a string in a text file

Well I want to search a text file that has some text in it and I want to use a pattern to serach for it. My code does this pretty well for single letters or numbers, but when I try to put in a string "Gus" for instance, it says there are no matches at all even though I know there is at least one Gus in the text file which I attached. At first I thought maybe the break statement is cuaseing the problem, but that is not the case, becuase with out the break statement, it just preints out no match 3 times. What am I doing wrong here am I using the matcher wrong or is it the input or something? thanks for any help with this!
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.Scanner;
  3. import java.util.regex.*;
  4.  
  5. public class SearchTest {
  6.     public static void main(String args[])
  7.     {
  8.         LineNumberReader lineReader = null;
  9.       try {
  10.             String sStr;
  11.             System.out.println("Enter your search term");
  12.             Scanner sScan = new Scanner(System.in);
  13.             sStr = sScan.nextLine();
  14.             Pattern pat = Pattern.compile(sStr, Pattern.CASE_INSENSITIVE);
  15.  
  16.             System.out.println("Your searching for " + pat);
  17.             lineReader = new LineNumberReader( new FileReader("Book-text.txt"));
  18.             String line = null;
  19.  
  20.             while ((line = lineReader.readLine()) != null)
  21.             {
  22.                 Matcher matcher = pat.matcher(line);
  23.  
  24.                 if (matcher.find())
  25.                 {
  26.  
  27.                     String msg = "Found Match "  + line;
  28.                     System.out.println(msg);
  29.  
  30.                 }else{System.out.println("No Matches");}
  31.                 matcher.reset();
  32.             }
  33.  
  34.     }
  35.     catch (FileNotFoundException ex) {
  36.       ex.printStackTrace();
  37.     }
  38.     catch (IOException ex){
  39.       ex.printStackTrace();
  40.     }
  41.     finally {
  42.       try {
  43.         if (lineReader!= null) lineReader.close();
  44.       }
  45.       catch (IOException ex) {
  46.         ex.printStackTrace();
  47.       }
  48.     }
  49.  
  50.  
  51.     }
  52.  
  53. }
  54.  
Attached Files
File Type: txt Book-text.txt (503 Bytes, 347 views)
Feb 17 '11 #1
0 1632

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gema Gema | last post by:
I have a large collection of directories full of various files and am looking to create custom text files for the contents of each directory. Here is the situation: The directories are named...
3
by: Ron | last post by:
I have a text file with the following format (pipe delimited) |column1=value|column2=value|column3=value|column4=value|... |column1=value|column2=value|column3=value|column4=value|... I have...
5
by: Johnny Meredith | last post by:
I have seven huge fixed width text file that I need to import to Access. They contain headers, subtotals, etc. that are not needed. There is also some corrupt data that we know about and can...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
10
by: mjl1976 | last post by:
I have textBox1 which is the string i want to search in file.txt Button1 I want textBox2 to show the line of text i am searching when i click button1
7
by: theyas | last post by:
How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE I've asked about this before and didn't get a...
11
by: F. Michael Miller | last post by:
I'd like to copy the listing of a directory (& sub directories) to a text file in vb.net. I'm looking for teh equivilant of the DOS command dir /s > TargetFile.txt. Thanks!
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
1
by: stoogots2 | last post by:
I have written a Windows App in C# that needs to read a text file over the network, starting from the end of the file and reading backwards toward the beginning (looking for the last occurrence of a...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.