• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer, countTokens, nextToken???

 
Bill Mackie
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write an application which will read data.txt file
and and report all words that have 5 or more letters.
Where am I going wrong? Thanks in Advance.
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ November 28, 2004: Message edited by: Dirk Schreckmann ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Well, you've almost got a program that will write out the first four words. The middle part of a for loop -- i.e., your "letterCount > 4" -- is a test the loop is supposed to do; as long as the test is true, the loop will execute, but as soon as the test is untrue, the loop will stop. Since letterCount starts at 0, letterCount > 4 is never true, so the loop doesn't run even once.

Of course, when you change that (you could use < instead of > , your program still won't do what you described, but it will do something, and that's a start!

Here's a hint for the next step: in addition to a loop, you're going to need to use an "if" statement.
 
Bill Mackie
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank-you for the reply and the welcome. Wish I would have stumbled across this site 12 sessions ago, 3 sessions left to go, with one program and one final exam.

I did as you suggested, was able to get app to output all the words in the text file. Can't seem to correctly code the if statement.....tried everything but the correct combination.


Have to upload to school server by 10:00pm tonight (11/28)
Thanks for suggestions, will be posting soon regarding program due next Wednesday.

Bill

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ November 28, 2004: Message edited by: Dirk Schreckmann ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, not sure about your deadline -- depends where you are, I guess.

Anyway, you've got



There are two things wrong here. First, you're calling nextToken() twice per loop iteration, so you're effectively discarding every other word. So let's eliminate that...



Now, this still doesn't compile, and for the same reason: you can't compare "word" (which is a String) to the number "4". What you want to do is compare the length of the String in "word" to 4. The class String has a member function which returns the length of the String -- you can look it up here in the online API docs. Let's call this method xxx() (that's not the real name, you have to go look it up yourself!) You'd want to say something like



This will now compile (if you replace xxx with the right method name!) and will only print each word if it has more than 4 letters.

I don't know if this is exactly what your assignment was supposed to do, but this is certainly better than nothing.

The online API docs are your friends. Browse them frequently and learn from them.
 
Bill Mackie
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank-you for your help. As soon as I saw how you wrote word.xxx, I knew right away xxx = length. App runs as it should, thanks again.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Mackie:

Have to upload to school server by 10:00pm tonight (11/28)
[ November 28, 2004: Message edited by: Dirk Schreckmann ]

Is that 10:00 p.m. in Siberia?

Sorry, I didn't get in on this conversation in time to help you. It's good to see that you got it working, though. Good luck on your next program. Feel free to come back for more help. Maybe I'll be able to chip in next time as well. (Of course, Ernest probably gives better advice than I do, anway.)

Keep coding!

Layne
 
Bill Mackie
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eastern Standard Time......Michigan to be exact. We have longer and colder winters.

Thanks for the offer Layne, I will be posting for my last program after this reply. I wish I would have stumbled across this site @ the begining of the semester...I feel I will have to sit in JAVA II another semester to strenghten my BRAMES in preparation for Network Programming.

Hope to hear from you and Ernest.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic