aspose file tools*
The moose likes Java in General and the fly likes How to use the regex while reading from text  file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to use the regex while reading from text  file" Watch "How to use the regex while reading from text  file" New topic
Author

How to use the regex while reading from text file

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

Hi,
In this below code I am trying to read the file and then use the pattern to but when using the matcher the output is not coming according to pattern used instead output is coming as the text in the text file


Text File i am using with file name is Names.txt
Upanshu,Rahul,Rohit,Rajesh,Ranvir,Ramesh,Ishan,Sachin,Kinder,Joy,Manu,Mahesh.
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 242

First off, if you want people to spend the effort to answer your question, put some effort into cleaning up your code (i.e. indent). You may also find your problem that way (hint, line 17). I think you need to start over, there are a number of errors in the code, such as reading in the text but doing nothing more that printing it out.


Sent from my IBM 360 mainframe
upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

This is the thing I am asking that how to how to use the regex while reading from file so that data in file can be searched in file
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8419
    
  23

upanshu vaid wrote:This is the thing I am asking...

Upanshu,

Please read the posts of others before you reply. You were asked politely to clean up your indenting, but your next post is even worse than your first.

1. Do NOT use TABs to indent.
2. As Carey already said, you will get more responses if you take the time to clean up your code before you post (you can use the 'Edit' button to do this now that you have).
3. DontWriteLongLines. It makes your posts very hard to read. Yours are OK so far, but the excessive indenting doesn't help.

I'll be happy to help out when I've seen some evidence that you've read our requests.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73


Apology Winston , kindly tell the solution to the problem i have mentioned.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8419
    
  23

upanshu vaid wrote:Apology Winston , kindly tell the solution to the problem i have mentioned.

OK, well first up: we don't "tell solutions"; we help you to find your own.

So here's a few suggestions:

1. Your indentation is still out of whack (but admittedly, a lot better).

2. What does FileReader.read(char[]) do?
There's no point in simply using a method in the hope that it'll work. Since the docs, admittedly, aren't great, I'll tell you: In your case, it will read in exactly 90 characters every time it's called, except (probably) the last, since you've supplied it with an array of capacity 90.

3. Line 11 is redundant, since the result of c.length will always be 90.

4. I suspect very strongly that what you're trying to do could be far more easily done with String.split(), so I suggest you look at the docs for that method.

HIH

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

It also sounds like you are trying to code without thinking about what you want to do. I'd advise you to StopCoding (<--click that).

THINK about what you want to do. Only when you know what you want to do should you figure out HOW to do it. Any time someone says "How do I use THIS to do THAT", I am pretty sure they don't know what they want to do, but heard about THIS somewhere in a vaguely related way and now want to use it to solve their problem. It's like someone heard you can use a hammer to build a house. They need to weld some copper pipes together in the house they are building, and now want to know how to use the hammer to weld the pipes together.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 242

I personally prefer to use BufferedReader and its readLine() method when reading text files. Then you will be dealing with Strings as opposed to char arrays. Then if you have a comma separated line of text you might want to use the String split() method instead of regular expressions.
 
wood burning stoves
 
subject: How to use the regex while reading from text file