• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching Strings in a textfile  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've been working on this project for a couple days now, and it seems like I am getting no where. I have written data to a textfile and I am trying to read it from the textfile and compare two Strings from it to a users response. Any pointers would be greatly appreciated. Thanks.
This is the portion that I have dealing with searching the textfile.

The textfile is formatted as follows ()is what I want to extract
Kind kind)
Name:name
Weight:weight
Color color)
Garbage:garbage
Kind kind)
Name:name
Weight:weight
Color color)
Garbage:garbage etc...
Thanks again
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell me if I'm wrong but,
won't hasMoreTokens always returns false ?

I think you should put
StringTokenizer kindtok = new StringTokenizer(kindcheck,":");
into the while() loop.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should use JDK 1.4 regular expression(RE) for doing that sort of search. Sun has NIO examples at http://java.sun.com/j2se/1.4.2/docs/guide/nio/example/Grep.java.

OR you can use RE for you code like:
 
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely the

StringTokenizer kindtok = new StringTokenizer(kindcheck,":");


Should be inside the loop. But I do not understand the text file. some have "(kind)" and some with color:color. What you really want to tokenize on? Require more clarification on your text file.
 
Al Stumpel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,for the text file basically I want to extract akind and acolor which occur after kind: and color:. So the output would be:

Kind:akind
Name:aName
Weight:aWeight
Color:aColor
Garbage:aGarbage

It then repeats right below this with a different kind that is stored and it's name and so on.

I want to extract aName and aColor and compare them to two String values a user has inputed.
[ March 28, 2006: Message edited by: Al Stumpel ]
 
Al Stumpel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been playing around with the code trying to find a solution, but I think I may have made it worse. Here is the current version. I symplified it by making it a method in its own class.

Here is the textfile I am using.

The user would input Scewdriver and Silver, and I am trying to search and return that the item is in the database.
Thanks. Any help is greatly appreciated.
[ March 28, 2006: Message edited by: Al Stumpel ]
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just slightly modified yor class to get something working. You need to test it properly and improve on it.


when you read a line like this "name:screwdriver" from your text file and you call

token.nextToken() --> you get "name"
token.nextToken() --> you get "screwdriver"

 
Mahadevan Gorti SS
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here another approach with RE. You have to test it thoroughly


Output for above program for input from f:/vob/test/data.txt is:
Usage: java [-DFILE=F:/vob/test/data.txt] REKindColorMatcher kind color

========= 1===========@0
Kind:Screwdriver
Name:Screwdrive
Weight:5
Color:Silver
Garbage:No


========= 2===========@131
Kind:Screwdriver
Name:Screwdrive
Weight:50
Color:Silver
Garbage:Yes


========= 3===========@355
Kind:Screwdriver
Name:Screwdrive
Weight:501
Color:Silver
Garbage:No


========= 4===========@428
Kind:Screwdriver
Name:Screwdrive
Weight:51
Color:Silver
Garbage:No
[ March 30, 2006: Message edited by: Mahadevan Gorti SS ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!