• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringTokenizer(read from text File) help needed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to reset "sum" to zero whenever you have output the result of a summation.
 
Lu Kim Hui
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ??
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Lu Kim Hui
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks alot guys! really appreciate for the helps
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome What have you got working now?
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic