• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use the regex while reading from text file

 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1685
22
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Apology Winston , kindly tell the solution to the problem i have mentioned.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Bartender
Posts: 1685
22
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic