aspose file tools
The moose likes Beginning Java and the fly likes inner class or new class or new method? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "inner class or new class or new method?" Watch "inner class or new class or new method?" New topic
Author

inner class or new class or new method?

joyita raksit
Greenhorn

Joined: Oct 12, 2006
Posts: 17
hey ho,

i'm trying to write a bit that basically takes in a random sentace (going to expand it to take input later, for now, defined a string) and returns a random word.

my problem is that, i'm unsure how to get the countTokens variable in so i can multiply the math.random method by it to get my random word index.

here is the code;




so do i need to create a new class? an inner class? or a new method? i understand why it's going wrong because my variable ac has not yet been defined. i cannot make them unstatic, as my main static method refers to them & cannot refernce non static...
aaaargh, i think i'm seeing the problem okay, i just cant find my way to the solution!

cheers very much for any advice x
joyita raksit
Greenhorn

Joined: Oct 12, 2006
Posts: 17
oh yeah, and if anyone could give me pointers on how to do this stuff with regex, i think it's the preferred way? using string.split? or am i making that up?
joyita raksit
Greenhorn

Joined: Oct 12, 2006
Posts: 17
okay, given the regex version a go, but i keep getting missing return statement for line 22, then i chuck in a return items and it cpompiles but when i run it i get a series of blanks or an arrayindexoutofbounds exception?? what am i doing wrong??? oh yeah, and the previous question regarding getting the length remains unresolved, i just wandered off & found myself a new one...

anyway, the code, please please help...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19



First, you used the wrong values. The regex gets compiled, and the string gets split. You compiled the string and split the regex.

Second, what is the purpose of the "for" loop?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
joyita raksit
Greenhorn

Joined: Oct 12, 2006
Posts: 17
ahhh, you beauty! so here's the new & improved. still got the problem of getting the token length in order to generate the random word index (cos the string will be user input in the end) any pointers?

& here's the new code for reference

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
oh yeah, and the previous question regarding getting the length remains unresolved, i just wandered off & found myself a new one...


Don't pass the index to the getWord() method. Let the getWord() method pick the random index.


[ November 26, 2006: Message edited by: Garrett Rowe ]
[ November 26, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
joyita raksit
Greenhorn

Joined: Oct 12, 2006
Posts: 17
cheers garrett, thats helped brilliantly, really enjoying this & cant wait till i've some knowledge to share back
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: inner class or new class or new method?
 
Similar Threads
read specific line from text file
another NullPointerException problem
word on random location
Small problem with arrays in Hangman java game
br.readLine()