Results 1 to 5 of 5
  1. #1
    Kenken is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default display random words from a string

    Hi,
    I'm having trouble making up code to show a given number of random words from a given text String..

    This is what I've been able to do so far. It's supposed to display 4 random words from the String text, but what it does is instead show 4 random numbers going from 0 - text.length(). I feel like I'm almost nearing solution? But I don't know how to change those random numbers into random words from the String instead...
    PHP Code:
    code
    I'd appreciate all suggestions.
    Last edited by Kenken; 04-09-2011 at 01:18 PM.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Well, the program does exactely what you stated, because you generate a random number, convert it to a string, then display it. What I would do, is get an array of words using Strings split() method, generate 4 unique numbers, and display the words in those array indexes.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Kenken is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    I'm still a newbie at Java so I haven't come to arrays or splits yet. I'm still learning the very basics, so I'd like some suggestions to correct my code simply..

    I tried to generate a random number from the string and then put it in the scan.hasNext to give me a word in place of that random number I got. And to put it in the hasNext, I had to convert it to a string, because it wouldn't allow me putting an int-type. That's what I thought but apparently that's wrong. So what can I do now? ;/

  4. #4
    Kenken is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Noone can help me in this case? I really don't know how to move on from this, tried several ways..

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Kenken View Post
    I'm still a newbie at Java so I haven't come to arrays or splits yet. I'm still learning the very basics, so I'd like some suggestions to correct my code simply..
    I'd suggest checking the tutorials, your texts and the API and reading up on use of arrays, and String#split(...) (as suggested by moonchile) as these offer your best hope for a straightforward solution.

Similar Threads

  1. Removing words from a String
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-15-2011, 11:50 AM
  2. Random selection of words
    By netspinner in forum New To Java
    Replies: 3
    Last Post: 12-14-2010, 06:31 PM
  3. Cannot display a random string from .dat file
    By explosion242 in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 02:48 PM
  4. Replies: 4
    Last Post: 07-07-2008, 02:32 PM
  5. Analyze a string of words
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 11:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •