File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes StringTokenizer(read from text File)  help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer(read from text File)  help needed " Watch "StringTokenizer(read from text File)  help needed " New topic
Author

StringTokenizer(read from text File) help needed

Lu Kim Hui
Greenhorn

Joined: Jul 06, 2012
Posts: 16
sales.txt
3000+200
1000
2000+1+2
5000



What I want the text file above display a new result which means sum them up whenever detect a "+" . Can someone please guide me how to code?
3200
1000
2003
5000

the result of the following code is
3200
1000
5203 <<< because sum = sum + first line result how to avoid this happen?
5000







Class SalesCounter



Class SalesCounterDemo

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
You need to reset "sum" to zero whenever you have output the result of a summation.


Ping & DNS - my free Android networking tools app
Lu Kim Hui
Greenhorn

Joined: Jul 06, 2012
Posts: 16
Ulf Dittmer wrote:You need to reset "sum" to zero whenever you have output the result of a summation.


umm.. i add sum = 0 after line 50 ,the results shows 1st and 3rd line 0... any more ideas ??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
Why are you using tokenizer at all? Have you read its documentation? What about The String "\\+" is a regular expression; you have to escape the + with \\ because + is a meta-character.
Why are you asking for a file name and later checking whether it is the same as a "sales.txt"? That is a bit strange. If you are going to using that file, why are you asking for its name?
Why are you giving the line the name a and the number the name line? That is a potent source of confusion. It might be better to call an integer i.
What happens when you try to get an int out of a line which contains the + before you split the line?
Why are you using indexOf rather than the contains method on the String?
Why are you reading the line twice in the loop? That is brittle code which depends on the file having + only in odd-numbered lines, and always having an even number of lines.
You appear not to have noted the suggestions I made yesterday. What happened when you tried to create a new Scanner and read from System.in? You will have to read something to see the effect properly.
Lu Kim Hui
Greenhorn

Joined: Jul 06, 2012
Posts: 16
Sorry i am new to java, this is my exercise given by my lecturer which must meet the requirement by using
o Use .equals(“”) from String class to make sure it is not reading an empty line
o Use .indexOf() from String class to verify whether the ‘+’ exist in each line.
If the ‘+’ exist use StringTokenizer token = new StringTokenizer(value, “+”)
to extract the numbers. Also use token.hasMoreTokens() to iterate through the numbers.


Well, this topic is abit confusing for me... that's why i am seeking for guidance

About the file name checking, it's my previous exercise which need me to do so... and this is just a add on exercise by using indexOf and StringTokenizer

[UD: For clarity, I removed your quote of Campbell's post. Quoting an entire post isn't useful, especially if you're posting right after it. ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
Lu Kim Hui wrote:i add sum = 0 after line 50

That's the wrong place. You need to do that after a summation is done, not right in the middle of it.
Lu Kim Hui
Greenhorn

Joined: Jul 06, 2012
Posts: 16
Ulf Dittmer wrote:
Lu Kim Hui wrote:i add sum = 0 after line 50

That's the wrong place. You need to do that after a summation is done, not right in the middle of it.


thanks! it works, adding after line 56
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Lu Kim Hui wrote:this is my exercise given by my lecturer...

Well you might want to tell him from us that StringTokenizer is legacy code; and you can do exactly the same stuff (and a lot more) with String.split(), Pattern (java.util.regex.Pattern), Matcher (java.util.regex.Matcher), or indeed new Scanner(String).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Lu Kim Hui
Greenhorn

Joined: Jul 06, 2012
Posts: 16

ohh...i i din't know about split/pattern/matcher.. is it same with StringTokenizer? cause currently what i am learning now is StringTokenizer ,but willing to learn more new codes if got that chance
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Lu Kim Hui wrote:ohh...i i din't know about split/pattern/matcher.. is it same with StringTokenizer?

No. Completely different classes.

cause currently what i am learning now is StringTokenizer

And what we're trying to tell you (and maybe your lecturer) is that it's old-hat.

but willing to learn more new codes if got that chance

Then I suggest you look at the API, and then maybe the tutorials. It's also possible that regexes are something you'll be doing later on in your course, but I have to admit being surprised that a curriculum still teaches StringTokenizer; 'chances are you'll never use it again after these lessons.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
It really looks as if he were being taught Java1.3 with a few new features like Scanner tagged on. I might have used Scanner throughout, but the + would make it awkward.

I know you disagree about Scanner, but it is instructive for the newbies to see different opinions and disagreements like that.
Lu Kim Hui
Greenhorn

Joined: Jul 06, 2012
Posts: 16
thanks alot guys! really appreciate for the helps
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
You’re welcome What have you got working now?
 
jQuery in Action, 2nd edition
 
subject: StringTokenizer(read from text File) help needed
 
Similar Threads
read one file to write multiple files
Using StringTokenizer to perform Arithmetic Operations on Strings
Object in a file
java.lang.NullPointerException
how to read one file to write multiple files