Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

inner class or new class or new method?  RSS feed

 
joyita raksit
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
joyita raksit
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
joyita raksit
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers garrett, thats helped brilliantly, really enjoying this & cant wait till i've some knowledge to share back
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!