GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes StringTokenizer, countTokens, nextToken??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer, countTokens, nextToken???" Watch "StringTokenizer, countTokens, nextToken???" New topic
Author

StringTokenizer, countTokens, nextToken???

Bill Mackie
Greenhorn

Joined: Nov 27, 2004
Posts: 9
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Bill Mackie
Greenhorn

Joined: Nov 27, 2004
Posts: 9
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Nov 27, 2004
Posts: 9
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Bill Mackie
Greenhorn

Joined: Nov 27, 2004
Posts: 9
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.
 
GeeCON Prague 2014
 
subject: StringTokenizer, countTokens, nextToken???