This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java Writer, Java Filer, Tokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Writer, Java Filer, Tokenizer" Watch "Java Writer, Java Filer, Tokenizer" New topic
Author

Java Writer, Java Filer, Tokenizer

brando brandido
Greenhorn

Joined: Jan 11, 2012
Posts: 20
Will anyone please help me with my java problem,

Create a program named SalesSummary that reads its input from a text file named sales.txt.

The program will then have a method that will separate and display the tokens in this table format below:

       Product 1    Product 2    Product 3

Day 1    5000     5859.99    6001.75
Day 2    5555     7750.99    6000.50
Day 3    5877    5559.30    6578.98

Aside from displaying the sales, write your output in a text file named salesFile.txt
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
What have you done so far? What is in sales.txt(format)?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
brando brandido
Greenhorn

Joined: Jan 11, 2012
Posts: 20
this is what's inside the sales.txt



what i got so far is this but it aint correct



the text just went every line

brando brandido
Greenhorn

Joined: Jan 11, 2012
Posts: 20
the output should be a 4 rows 4 columns, just what i've said on my first post.

the the first column is day 1, day 2, day3
the second column is Product 1, 5000, 5555, 5877
and 3rd colum is Product 2, 5859.99, 7750.99, 5559.30 and then 4th column is product 3 and so on...

so on first row, we have a blank column then Product 1 column, Product 2 column, Product 3 column
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
One suggestion,
Don't read and simultaneously try to write into another file.
Use data structures, (either an array or array-list). So it would be like, you will first read data from your input file, process it and store it in data structure. And later, write the data from data-structure into your another file, (in whatever format you want).
Korhan Rankin
Greenhorn

Joined: Jan 05, 2012
Posts: 14
Akhilesh Trivedi wrote:One suggestion,
Don't read and simultaneously try to write into another file.
Use data structures, (either an array or array-list). So it would be like, you will first read data from your input file, process it and store it in data structure. And later, write the data from data-structure into your another file, (in whatever format you want).


that was my first thought too when i saw the code.. read, process then write
Ryan Alfeche
Greenhorn

Joined: Jan 16, 2012
Posts: 5
Try this one..
in a main method create a loop..


Hope this one will help kuya.. hehe
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
Welcome to the Ranch Ryan Alfeche.

Apart from the poor indentation and spelling, which makes your code difficult to read, there are some serious errors in it.
You ought not to put lots of code in the main method like that.
You should not make your methods static as a default.
I think your regular expression will fail to do what you think it does.
Your use of the substring method ties you to a particular implementation and format.
You ought to know not to use StringTokenizer in new code; you will find out why from its documentation.

Brando Brandido, you are not thinking object-oriented. You don’t want to move lines from file to file. You want to create a Product class with details of the sales from different days, and write the details of that object into your new file.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
Actually I think I may have the classes the wrong way round; you might prefer to display the different Products for each Day object.
Ryan Alfeche
Greenhorn

Joined: Jan 16, 2012
Posts: 5
This is my code..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
Unless you have been told specifically to use CR-LF, you ought to avoid the \n and \r escape sequences.
Still non-object-oriented programming, I am afraid. Thank you for correcting the indentation.
brando brandido
Greenhorn

Joined: Jan 11, 2012
Posts: 20
@Ryan Alfeche

thanks a lot! it works.

but, i am beginner, and i am afraid i won't be able to defend the code if a person asks me about the role of for loop and while loop

can anyone further explain line by line the code? lols

i don't seem understand yet the substring?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ryan Alfeche wrote:This is my code..


Please don't do the OP's work for him. It doesn't help him. This site is NotACodeMill.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

brando brandido wrote:@Ryan Alfeche

thanks a lot!


You shouldn't thank him. He didn't help you. You should DoYourOwnHomework, or you won't learn anything.

but, i am beginner, and i am afraid i won't be able to defend the code if a person asks me about the role of for loop and while loop


Case in point.

can anyone further explain line by line the code? lols


No. The idea is that you study the topics, such as looping, etc., and then write the code yourself, and then you'll understand it. Of course, if you get stuck along the way, if you show your work and ask a specific question, somebody will help you get over the hump.

i don't seem understand yet the substring?


Please SearchFirst and ShowSomeEffort.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7505
    
  18

Jeff Verdegan wrote:No. The idea is that you study the topics, such as looping, etc., and then write the code yourself, and then you'll understand it.

@brando: And furthermore, nobody in their right mind is going to expect you, as a beginner, to get it right first time.

You will learn far more by trying and failing than you ever will by copying somebody else's supposedly 'correct' code.
Also, a good teacher can smell plagiarism at 100 paces in a darkened room with their eyes shut.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
Jeff Verdegan wrote: . . . Please don't do the OP's work for him. It doesn't help him. . . .
Least of all that code, which I have already criticised.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
Winston Gutkowski wrote:. . . Also, a good teacher can smell plagiarism at 100 paces in a darkened room with their eyes shut.

Winston
Round here we use software which searches the web for text identical to what is handed in. It is very sensitive and would find plagiarism of that code in a few seconds, resulting in a 0 mark for the whole assignment. Particularly if I leave that post visible for the software to find . . . mwaahaahaahaa!
brando brandido
Greenhorn

Joined: Jan 11, 2012
Posts: 20
thank you guys,

i really appreciate all your advice and in fact i not copied the whole code ryan alfeche posted because i am afraid i won't be able to explain it if my teacher would ask me to explain the code. lol so i am careful.

i am studying more about substring and reviewing for and while loop,

will the moderator please close this thread please? or simply delete it

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
brando brandido wrote:thank you guys,
You’re welcome
brando brandido wrote: . . . not copied . . . i am afraid i won't be able to explain it if my teacher would ask me to explain the code. . . .
If I had thought the code was any good, I’d have deleted it long ago
brando brandido wrote:will the moderator please close this thread please? or simply delete it
No. We do not usually close nor delete “good” threads. Other people are free to add comments later on, and this thread’s information might be useful to other people in the future. Besides, the thread will already have been indexed and recorded somewhere on the net.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
This is what you posted elsewhere, but I think this is a continuation of this thread. Please note this FAQ.
brando brandido wrote:hello,

i have some text in notepad that looks like this



i am ask to create a program to call the notepad and make the output looks like this

       Product 1    Product 2    Product 3

Day 1    5000     5859.99    6001.75
Day 2    5555     7750.99    6000.50
Day 3    5877    5559.30    6578.98

i posted this in a different thread but seems the suggestion are too advance for me? i just want it simple and easy to explain.

i am a bit confuse, so i hope you guys can advice me

so so far what i've done is this;



the problem with the above code is that, the amount "5000" won't appear,

the output will look like this;


       Product 1    Product 2    Product 3

Day 1     5859.99    6001.75

Question: what should i do in order that the amount "5000" will be included in the second FOR loop?


so in my own way, for me to get the "5000" amount i modified the code to like this, and that's the time i got all the amount on day 1.



See, I told you somebody else might post.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
Why are you using StringTokenizer? If you click the name of the class, that will turn into a link to its documentation where it tells you not to use it.

Java™ is supposed to be an object-oriented language, so you ought to be thinking of objects. I also think you should stop programming until you have worked out what you intend to achieve. You need some high-tech stuff, involving approx 1.00369025e23 components. That is how many atoms there are in a sheet of A4 80gsm paper You will also need a soft pencil and an eraser; the latter is far and away the most important piece of equipment at this stage

Find a copy of Effective Java by Joshua Bloch, and in it you will find a chapter warning against using Strings. They are nice to display and read, but awkward to handle. What will happen to your substrings if you get to day 10? You will be all out of phase with yourself and will probably suffer Exceptions because you are trying to get a number out of something like “0 1234.56”
I think you need a DaySales class and a ProductSales class. I think they can be very simple. You need to work out which sales you had of which product on which day, so you can work out which fields you need in each class. Draw diagrams rather like in this post or this one (about different problems), and your arrows will show you wich object connects to which. You can have a DaySales object connecting to several ProductSales objects, or vice versa. If you are sure in advance how many there will be, you can use arrays, otherwise you can use Lists.
If you get the diagrams into the right shape, it will match the shape of printout you require.

Then you can work out how to get the text into those objects. Once you have the diagram, it ought to be simple to envisage.

And this is not advanced stuff. This is beginner’s stuff. If you know about BufferedReaders, you ought to have been taught how to create your own classes and instantiate them.
brando brandido
Greenhorn

Joined: Jan 11, 2012
Posts: 20
sorry but our topic is all about Strings, and the teacher told us to use StringTokenizer, substring, tokens.

that's why i stick to tokenizer.

there might be a follow up discussion after this.

thnks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
You can use tokens rather than substrings.
 
Don't get me started about those stupid light bulbs.
 
subject: Java Writer, Java Filer, Tokenizer
 
Similar Threads
Passed OCMJD.
how to identify loop in a linked list
parent child relation ship
Regular Expression
Need Help With Dynamic text box in JSP