Results 1 to 10 of 10
Thread: Random Strings
- 01-22-2011, 10:59 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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
- 01-22-2011, 11:25 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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)
- 01-22-2011, 11:29 AM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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...
- 01-22-2011, 11:38 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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.
- 01-22-2011, 11:40 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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]
- 01-22-2011, 11:50 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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...
- 01-22-2011, 11:54 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
I wrote that in the Fish constructor
Then i wrote a method called setTypeJava Code:int a2 = rand5.nextInt(1); if(a2==0) { type = "herbivore"; } else if(a2==1) { type = "carnivore"; }
does it seem right?Java Code:public void setType(String tp) { type = tp; }
- 01-22-2011, 05:19 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
anyone? is it ok?
- 01-22-2011, 06:44 PM #9
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
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
-
Where are you getting a random int and using it in an array to get the String? This has already been suggested to you I believe.
Similar Threads
-
using random strings from arrays insult generator
By carden2 in forum New To JavaReplies: 5Last Post: 04-05-2010, 05:51 AM -
how i use the random class to random the cards i have
By yanipao in forum New To JavaReplies: 14Last Post: 10-19-2009, 10:57 AM -
Random Strings help!
By hhale in forum New To JavaReplies: 5Last Post: 09-23-2009, 12:57 PM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks