Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes help with creating random strings in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with creating random strings in an array" Watch "help with creating random strings in an array" New topic
Author

help with creating random strings in an array

Corbin Blake
Greenhorn

Joined: Nov 10, 2004
Posts: 22
Hi all, (first time poster...long time reader)

I am having trouble getting the random concept down. In my assignment, i have to create a random strings of user determined length and number. I have no problem with the iteration, but when it comes to creating the strings, thats when i don't have a clue. From what i have read from the book and websites i think that it goes something like this:


Is that correct???


I hope you know your paying me for this!
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I'll give you a piece of code:
letter = alphabet[r.nextInt(alphabet.length)]

nextInt(x) returns an integer >= 0 and <x

This gives you one random letter.

You can assemble a String from letters using StringBuffer, you can easily make StringBuffers into Strings with a String constructor, and you can put references to your Strings in a String[] array.
[ November 10, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Corbin Blake
Greenhorn

Joined: Nov 10, 2004
Posts: 22
I am still having a lot of trouble with Random... when i enter the code snippet that you gave me among with other code. I keep getting null exceptions, or the first letter of my array. I am completely at a loss.
Any help would be appreciated.

Thanks

Corbin
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Since this is for an assignment, we wouldn't want to do your work for you and give you the code. Post your code and we'll try to point you in the right direction.


Junilu - [How to Ask Questions] [How to Answer Questions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are days when any of us would be happy to make one or two lines of code run! What's the simplest thing you can possibly try? Maybe:

just to get a feel for the ints you're going to get.

If you want to use one of those ints to pick a random letter out of the alphabet you'll probably use the int as an index into your array of letters. That's a fine idea, but look at those ints. Some are going to be too big for your array. Read up on Random javadoc to see how to get numbers in the range you want ... 0 through number of letters minus 1.

So try a few more lines:

If you get that going, put it in a for loop to get the right number of letters.

Any of that help?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Corbin Blake
Greenhorn

Joined: Nov 10, 2004
Posts: 22
Okay, i got a little further in my program, now the problem is that i need my for loops to print out strings like:

ATTAGACATTAC
ATTAGAAATCCC
CCCATACACACA
...etc.

What I am getting is.

ATTAGACATTACATTAGAAATCCCCCCATACACACA

what am i doing wrong?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
It looks like you're just putting everything into one big String (DNA). Do you want to put them into a String[] instead? After your inner loop, put DNA into an array., and then reset DNA to """.

Your method should probably return a String[] instead of a String, too.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

>> ATTAGACATTACATTAGAAATCCCCCCATACACACA
>> what am i doing wrong?

What Joel said.



There's really no need to test for length >= seqArray.length inside the loop. In fact, I don't see a need for the seqArray at all in the code you posted. Also, I would take Mike's suggestion and use a StringBuffer. And you should not be using the "magic number" 4. The argument you pass to nextInt() should be the length of the gene array. I would also break out the inner loop into its own method:

yuqing ma
Greenhorn

Joined: Nov 10, 2004
Posts: 11
Here is an good example
code:

class RandString{
private static String ssource =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static char[] src = ssource.toCharArray();
public static class
RandCharGenerator implements CharGenerator {
public char next() {
return src[r.nextInt(src.length)];
}
}
public static class
RandStringGenerator implements Generator {
private int len;
private RandCharGenerator cg = new RandCharGenerator();
public RandStringGenerator(int length) {
len = length;
}
public Object next() {
char[] buf = new char[len];
for(int i = 0; i < len; i++)
buf[i] = cg.next();
return new String(buf);
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with creating random strings in an array
 
Similar Threads
invoking methods while inside methods
Programming Diversion 2a: Applying a Key
Java array help
String to char problems
[SOLVED]How to randomly get a String from an array in a random amount of times.