This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, this is my first post here. I've enjoyed reading back into the archives and have gotten a lot of helpful info. I'm in the fourth week of an online jave course. I havent done anything with my computer previously other than run software and check email. I am having a lot of fun and sleepless nights with Java so far. Everything so far has come to me after a little thinking, googling and reading the Sun site and other forums, but now I'm stuck. I just cant seem to figure out what to do.
The project is... using a while loop, take user data till a sentinel string is entered and print the data to a local file. Sounded really easy at first. I managed to get the while loop working, but I cant for the life of me figure out how to get the data collected into one file. All the data is entered as String type. I've seen alot of examples using loops of various kinds on primitive type data, but not much on Strings. Here is what I have so far:
I would appreciate any kind of shove in the right direction.
Then in the loop, after you get the input use the proper method in PrintWriter. hint you need to print a line. Check the API for a good candidate.
Just one simple line in the loop will solve this, if you put both inputs per loop on the same line. If they go on seperate lines, then 2 identical lines will be needed(or you can create a very small method to print to the file. [ September 28, 2006: Message edited by: Robert Hill ]
You're very near, pal. You just forgot to write you item and price into the outFile...
...before asking for a new item.
Joined: Sep 28, 2006
Thanks a bunch fellas. I tried out Edwin's suggestion first. Because it seemed like I could try it out immediatly.....Great. Damn it seems so simple now!
Is this what you had in mind??
It works great!!!
I think I was confused on the issue of what would overwrite the local file. I thought every time you added something new it would overwrite what ever was there previously. But now, I'm thinking... the file will only be overwritten when the file is closed and then opened again.......is this correct?
First thing I would do is wrap PrintWriter around FileWriter, then set the append flag to what you need.
PrintWriter outFile = new PrintWriter(new FileWriter("orderOut.dat",false));//allows for overwriting the previous file.
Change to true if you want to append.
Does this mean that I can re-open a file already written and have a choice to overwrite (is this a default?) or to add to the existing file?
While Edwin's was very easy to insinuate into what I already had written, I'm curious as to which way of doing it is "better". Most functional, most like the real world.
I thank you both for a push over my hump. After I post this I am going to get Robert's suggestion into my editor and play around with it. As a matter of fact play around with both of them and see what I can do and not do. As far as different ways to format, ect.