Hi, new here but would appreciate some sort of help.

I have to create a program that takes 20 random strings from a string array of 40 words I made, and store them into a new string array called second. Directions:

-create random # generator

-generate random # between 0 – 39

-take first[random#] store it into second

-strings[20] holds random strings (second)

So my problem is I don't know how to take the 20 random numbers, and store them, and than convert them back into 20 words from the original array. Any help would be appreciated. Thanks in advance.

``````public class stringTest
{
public static void main(String[] args)
{
Random num = new Random();
for (int i = 0; i < 20; i++)
{
int number = num.nextInt(39);
String [] second = number;``````

## All 12 Replies

You've sort of petered out in the middle of a loop. I think you've got the right idea, but you're stuck on the execution.

Some tips:
-if you have the array declaration in the middle of the loop, it'll overwrite itself each time, and you won't be able to address the array outside of the loop. Declare the array at the top of the method.
- do you want to store the index numbers you generate with your random call, or do you want to store the words you find at those indexes? I'd suggest the former, but that's up to you.
- to assign to an array, use something like second[n] = value, where "n" is the index number you want to use, or a variable holding that index number. You can use the index of your for loop (i) as the index of your array. This is good practice, as a matter of fact.

I see what you mean when you say to declare the loop at the beginning. What I was instructed to do: "Using a random number generator, choose 20 of these strings (this means generate 20 random numbers to choose the indices of the strings you want) to put in a second array (called second)."

Okay, so you're being asked to generate an array, called second, of 20 strings.

There's no mention of unique, so let's skip unique, but you might want to think about that as something to try later. How would you do that? There are a few ways, so maybe you should try to think of a few ways you could do it.

On the current problem:
To make your life a bit easier, break the problem down. If you can pick one random String from your source array, it'll be pretty easy to do it 20 times, so start by picking one random String and printing it to the screen.
Then, put that in a loop and do it 20 times.
Then, instead of printing them to the screen, put them into your array, and you're done.

Actually, as a zero step, you should make sure that you can tell when you're done. Before you do anything else, print out the members of your source array, so you know that it's populated, and so you know you can print out the members of an array. That way, when you get to the end, you can use the same technique, and you'll be able to tell that you've done it right.

I wanted to simply just use:

``second = words[number];``

But it is telling me:
Error: Type mismatch: cannot convert from java.lang.String to java.lang.String[]

I guess I can't do this but I don't know any other way? Which basically says I'm clueless, I suppose.

Not clueless, just new.

second is the name of an array. It points to an object which is a collection of Strings. (This is different from C - in C, the address of second is simply the address of the first object in second. Java doesn't work this way!)
words is also the name of an array, it also points to an object which is a collection os Strings.

words[number] points to a String, which is not an array.

So when you say `second = words[number]` you're asking the compiler to put a String into a slot which you've told it should point to an array of Strings. It rejects this, quite properly, because Java is fussy about that sort of thing. If you're trying to assign something to an array slot, it had better be an array. If you want to put a String into a slot, it had better be a String.

This should point you to your answer - I'm purposely not stating the final step, precisely because I don't think you're clueless. :)

Am I on the right track at least? Haha. I know what you mean, putting things in there appropriate slot. But I can't seem to figure out how to do such. Do I need to convert the String to something? Or how do I get them into the new array? I understand you not stating the final step, and I appreciate it. I'd rather learn than be given the answer.

Yes, you're on the right track. No conversion needed. You just need to be thinking at the right level.

I want to set the table - I have to put a fork and a knife and a spoon at each place. I go to the drawer, which contains silverware, and in the drawer I go to the slot for forks and I take out a fork, and I put it on the table. I don't put the drawer on the table, and I don't put the slot on the table...

You need to know which are the forks and which are the slots. (and later, when you do 2D arrays and inheritance, you'll have a drawer full of slots full of forks and knives and spoons, but that's later on).

I guess the problem I'm having is knowing the right code to use. I'm not sure what programming is needed.

No, you have all the tools. It's just a matter of keeping track of which thing is what. Play with it for a little while. Go back to that breakdown I suggested before: start by just reading out all of the members of the source array, then figure out how to pick just one of them and print it, then do that in a loop, and finally you'll worry about storing the values into your target array.

``second[0] = words[number];``

Would this be the correct code?

That will put the number'th element of words into the first slot in second, yes.

second[n] is a String, word[n] is also a String, so the assignment works.

Nevermind I see. I need to print second[0].

Aha. Thank you very much sir. I really, really appreciate it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.