Results 1 to 10 of 10

Thread: Random Strings

  1. #1
    Jimmy07 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default Random Strings

    Hi, I'm writing a program using BlueJ Java. I Need to select random numbers and strings. The random numbers are easy, but I can't figure out how to randomize strings. I'm can't use integers and then an if-statement.
    Can anyone help me for this? I tried to figure this out for while but nothing comes to mind.

    I'm using:
    import java.util.*;

    int i=0;
    String str = "";

    Random rand = new Random();
    i = rand.nextInt(4) + 1;

    Maybe I'm missing something or I'm unaware of a method.

    Thank you

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Someone else may have a better way to do this, but, here is my solution, you can generate random numbers from 0-25, then add 65 for capital letters, and add 97 for lower case.

    This uses ascii to generate random characters which you can convert to strings. Look up what you can do with characters to turn them into strings, then loop a random number of times, each time creating a new character and appending it to a string.

    Character (Java 2 Platform SE v1.4.2)

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    be creative !

    Fill a list/array with letters (A-Z, a-z) and generate a random number from 0 to array/list size and take the letter at that index...

    Or another idea could be to generate a random number from 97-122 for ascii lowercase letters (a-z)
    char c = (char)(rand.nextInt('z'-'a' +1) + 'a');

    Or for both (numbers and letters) you could use something like
    Long.toString(Math.abs(rand.nextLong()), 36);

    or or or...

  4. #4
    Jimmy07 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    By string I mean a word, like "blue" or "car"
    I thought of putting them in an array but I don't know if I'm allowed to. I'm trying to solve an exercise and the class has specific fields and methods I don't know if I can put an extra field. That's my main problem.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    what fields can you have? The only way I can think of to randomize Strings that already exist is to populate an array with it then generate a random number from 0 to array length - 1, then use array[random num]

  6. #6
    Jimmy07 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    there's a person before with the same problem his post's name is "fish constructor"

    I don't know how to post a uml...

  7. #7
    Jimmy07 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    I wrote that in the Fish constructor

    Java Code:
    int a2 = rand5.nextInt(1);
             if(a2==0)
             {
                 type = "herbivore";
                }
                else if(a2==1)
                {
                    type = "carnivore";
                }
    Then i wrote a method called setType

    Java Code:
    public void setType(String tp)
            {
                type = tp;
            }
    does it seem right?

  8. #8
    Jimmy07 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    anyone? is it ok?

  9. #9
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    u can put the string choices u want in array, and use the random class to generate an int from 0 to a number n , in ur case the length of the array, and then make it choose the number generated as the index for the the array element

    if u want, see my post, i had the same problem but it is solved now
    fish constructor

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

Similar Threads

  1. using random strings from arrays insult generator
    By carden2 in forum New To Java
    Replies: 5
    Last Post: 04-05-2010, 05:51 AM
  2. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  3. Random Strings help!
    By hhale in forum New To Java
    Replies: 5
    Last Post: 09-23-2009, 12:57 PM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

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