Hi I wondered if someone can help me with this I have text which is in a csv file and i want to read it in Incoming file: OrderID,CustomerID,ProductName,UnitPrice,Quantity,CategoryName 10248,clare,Queso Cabrales,21.0000,12,Dairy Products 10248,lauren,Singaporean Hokkien Fried Mee,14.0000,10,Grains/Cereals 10248,clare,Mozzarella di Giovanni,34.8000,5,Dairy Products 10249,lauren,Manjimup Dried Apples,53.0000,40,Produce and i want to put the output into another file called output, and i want the output in that file to look like this: clare spent 56.800 lauern spent 67.000 ---- 17 'Dairy Products' were sold 10 'Grains/Cereals' were sold 40 'Produce' were sold Could someone please help me i'm realy stuck thanks clare
Well, the first thing you will want to do is parse the file into something you can work with. You will need to use a StringTokenizer for that. Look here for how to use it Tackle that first, then post back your thoughts.
Clare, Parsing, I think is the easy part you can either go with a StringTokenizer as Chad suggested or you can use line.spilt() from the String class. Now, this is a simple text parsing problem and as such the right tool for that is Perl as in:
Assuming you have Perl simply put the above snippet in a file say PerlScript and say: perl �lnaF, PerlScript input > output Or if you can give execute rights to the PerlScript pending on whatever OS you have you can then simply say: PerlScript input > output I tried it here is what I got:
lauren spent 67 clare spent 55.8 CustomerID spent 0 ---- 40 'Produce' were sold 10 'Grains/Cereals' were sold 17 'Dairy Products' were sold 0 'CategoryName' were sold
A few notes
This is quick-n-dirty (should have some logic for the first line) but I think it suffices.
clare spent 55.8 (not 56.8 )
Then again maybe she spent 72.8 If you want this then remove the ; # from line 3.
If you really need the output sorted then simple insert �sort� before �keys�
Nevertheless, you can really do this in java and follow the same approach. The real power that I am using in the Perl script are hashes. I have one for the CustomerID (cust) and another for the CategoryName (goods). You can do the same in Java.
Set up as many streams you need to read a line from the file.
Split or tokenize each line. As in String values = line.split(�,�);
values has the CustomerId. Use that to get to your cust hash and tally up values (or values + values)
Same idea for the goods
Loop from 2-4 until EOF
print the results
I would do it like that. It would be fascinating to see the verbosity of an equivalent Java solution. Cheers. Leslie
It would be fascinating to see the verbosity of an equivalent Java solution.
I tried to retain as much Perl-like crypticism as possible in Java for the same basic algorithm. Mine reads from a file rather than standard input, but that's easy to modify if you like. And of course there's error handling to be added, but you didn't have that either. I suspect that when Java 1.5 eventually comes out this could be made a bit nicer looking using generics, autoboxing, and a foreach construct - but I guess I'm going to have to wait on that one. Hmmm... now I'm wondering how long a Jython solution would be. That's enough from me for now though.
"I'm not back." - Bill Harding, Twister
Joined: May 22, 2002
Jim, Thanks so much for taking on that charge! Jim, you certainly are a Java Guru and I bet that it took you no longer than ten minutes to write that code. I also know you did not test your code there is one compile error in the inc() helper -- D should be totD. Otherwise, it works. I tried to retain as much Perl-like crypticism as possible in Java for the same basic algorithm. Well, different strokes for different folks , that's why some people love (or hate) Perl! The crypticism is just the Perl way. On the other hand, the Java way is specifically the verbose way. When I said verbosity I really meant to follow the same algorithm the way I attacked the problem, but do it in the Java way. Moreover, despite you being cryptic with Java the code is still 3 times the size of the Perl solution, and believe me I could made it even shorter but I wanted to stay out of the obfuscation zone. (Yeah right, you are already there ) How about sorting the output? In Perl you simply would say:
Adding sorting was simple! Now let's see what how much we must add to the "CSVParse" code? I was making a case in point that the problem lends itself to Perl. BTW, originally I did it with a Perl one liner:
However, than my solution is good only on *nix boxes, and I wanted to provide a portable solution. So the real point was, its just much simpler with Perl. (Of course, once you're comfortable with the Perl way ) Not to mention that you still have the compile-and-run phase in Java In any case I hope we helped Clare. And Jim if you have a chance please modify the CSVParser to sort the output. Regards, Leslie [ February 21, 2003: Message edited by: Leslie Chaim ]
How to sort it with Java: Use 'TreeMap' instead of 'HashMap'. File size increase: 0 characters. By the way, I'm a fan of Perl. On the other hand, the author of a certain other Java certification site has been known to talk smack about Perl. Want to revive an old language war ?
Joined: Jan 30, 2000
I bet that it took you no longer than ten minutes to write that code. Ummm... something like that. It was spread across half an hour or so with various interruptions. The joy of multitasking. [b]I also know you did not test your code there is one compile error in the inc() helper -- D should be totD. Otherwise, it works. Oops. Actually I did test the code, but then made a few last-minute cosmetic changes to variable names. Obviously missed one. The crypticism is just the Perl way. Yeah, I just couldn't resist getting in a little jab there. Moreover, despite you being cryptic with Java the code is still 3 times the size of the Perl solution, and believe me I could made it even shorter but I wanted to stay out of the obfuscation zone. (Yeah right, you are already there ) I should've made this clear - I agree that Perl is more suited for this sort of thing. I was indulging in the Java version because I too wanted to see more clearly how the finished Java would look. And I have a definite appreciation for concise code; I often resent the extra verbosity imposed by Java. And if you look at the coding guidelines suggested in our own style guide, I personally would prefer to write my code in about half as many lines as I have to if I follow that guide. Well, 2/3 maybe. On the other hand, some Perl coders can really take this to extremes. In general, I like Perl for projects that are small enough for me to do everything myself - but I would be pretty worried to be on a big project with lots of other people sharing the code base, if it's in Perl. Too easy for everyone to obfuscate their stuff, intentionally or otherwise. I know it's possible to write clear, understandable code in Perl - but too few people seem to actually do it. And TIMTOWTDI is fun when you're writing the program, but a pain in the butt when you're trying to decipher one. Of course for me that's partly because I simply know Java a lot more than I do Perl; if I knew Perl better, I would be inclined to use it more. If I used it more, I'd know it better. One of those catch-22s I'm too lazy to work around. (And Larry did teach me that laziness is a virtue, so...) [ February 21, 2003: Message edited by: Jim Yingst ]