File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Manipulating a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Manipulating a text file" Watch "Manipulating a text file" New topic
Author

Manipulating a text file

Josh Priest
Greenhorn

Joined: Jun 06, 2007
Posts: 10
Here is what I have so far:




Here is an example of my input file: (store-orders.txt)

Items purchased by TexasStore:
Orders: 7 Unique: 4 OutofStock: 0
StockIDDepartmentIDInstances
34.341210
34.3126
2.75655


Items purchased by FloridaStore:
Orders: 6 Unique: 6 OutofStock: 1
StockIDDepartmentIDInstances
34.271278
33.3627
5.75733


I need something I can import to excel so I can sort by department ID.
My goal for the output file is something like this:

TexasStore34.3412
TexasStore34.312
TexasStore2.756
FloridaStore34.2712
FloridaStore33.36
FloridaStore5.757

Then if I needed a quick snapshot of all orders placed from department 12 (meats) from all stores I can just sort excel for the information.

My problem is that I can take the next value of a file and declare it something, but using regular expressions to get the stuff I need is not so easy.

The regular expression *a* will match abc, but not abcd etc.

Here is a botched pseudo code of what I want to do:



I suppose step one for me is figuring out java's regular expressions. I looked at some of documentation on it, but all the examples I found were too different than what I am doing and I am not experienced enough to bridge the gap.
[ December 02, 2007: Message edited by: Josh Priest ]
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Sounds like something I can really have some fun with, I like develop from scratch and am working on a similar problem and am not far ahead of you. I have been reading the book Mastering Regular Expressions Oreilly and strongly recommend the book.

A super-short lesson in regexes.



Quite a lot of work to do here, do you mind if your Thread becomes bulky ?


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Each "new Pattern(" should be "Pattern.compile(". Though for simplicity while Josh is learning, using String.matches() may be fine too. Note that matches() requires that the entire string match the regex. So nextString.matches("Items purchased by") will always return false. But nextString.matches("Items purchased by .+") may return true, since it allows (and requies) at least one character after the "Items purchased by".

\\w is not a word, it's a word character. Meaning a single letter, number, hyphen or underscore.

Josh, yes it will probably help to spend some time studying regular expressions now. The Java tutorial is a good place to start. Detailed info on regexes can also be found here (it's not Java-specific). And there are several good books out, including Mastering Regular Expressions as well as Java Regular Expressions. Time spent with one of these resources will be very rewarding in the long run.


"I'm not back." - Bill Harding, Twister
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282


O.P.'s request is using regular expressions to get the stuff I need is not so easy. in part, which bridging that gap is best not overloaded with bulky code sections on the first attempt, and is why I collapsed the pattern dot compile static method out. Further, my attempt does not know how to code in a spacebar character: ('   ') - which the sample dataset provided has in the line parsing sample. Additionally, we will probably have to split out the second numeric from the first as a named variable and capturing groups may appeal to the poster's logic or other approaches may be available. That is beyond my skills.

I suggest to the poster that we do the so I can sort by department ID., in Java. Once sorting is done, there are still likely ways to export the result to tabular data applications.

Java may as well have a tabular data application in the sample code base somewhere.

[ Message edit: ("[a-zA-Z]\\s\\d?\\.\\d?\\s\\d?") ?]
[ December 02, 2007: Message edited by: Nicholas Jordan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Manipulating a text file
 
Similar Threads
problem with writing file , please help
close() method; I/O class chaining
Getting the text out of a HTML?
New Problem
Reading the first word of each line from a text file and then adding them to a Combo box