views:

92

answers:

3

I need to write a program where the program would generate random letter and i would need to store this random character into an array

        char[] arrayRandom = new char[10];


        for (int i = 0; i < 8; i++) {
            randomNumLet = (generator.nextInt(20) + 1);
            System.out.print(arrayRandomLetter[randomNumLet] + " ");
            arrayRandomLetter[randomNumLet] = arrayRandom[i];
        }

is there anything wrong with my code? because when i run this and printed the array i get boxes for all the values in the array and there are some letter that this line of code cannot print

            System.out.print(arrayRandomLetter[randomNumLet] + " ");

Thanks

+5  A: 

You're assigning an element of arrayRandomLetter a value from arrayRandom. As you never initialize arrayRandom, its values are all 0. 0 is not the value of a printable character, hence the boxes.

An easy way to pick a random character is like this:

String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char randomChar = chars.charAt(random.nextInt(chars.length()));
Mark Peters
so, how do i initialize arrayRandom ?shouldn't it be an empty array since i'm going to store values inside?
Jann
I don't know, are you going to store values inside? You've certainly not shown an intention to do that based on your code (hint: where in your code does arrayRandom appear on the LEFT side of an assignment?)
Mark Peters
oh i get what you are saying alreadyif i want to add a value inside the array i should only put on the left hand side and not on the right hand side?thanks a lot =)
Jann
+2  A: 

You are trying to print arrayRandomLetter before it is assigned.

fastcodejava
That too, though switching the lines wouldn't make a difference :-)
Mark Peters
@Mark - the output would be different (8 spaces). It is not correct, but it **is** different.
Stephen C
i has already assign arrayRandomletter by putting in letters inside the arrayand sorry the array should be 8 and not 10
Jann
@Stephen C: No. `arrayRandomLetter[randomNumLet]` is 0 before the line `arrayRandomLetter[randomNumLet] = arrayRandom[i]`, and is 0 after. So whether the println occurs before or after that line makes no difference given this specific example. Actually, that assumes that those elements have not been previously assigned a value. And if they had, then it's arguably more correct, given this code, to leave the println where it is :-).
Mark Peters
@Mark - didn't you notice the ''` + " "`'' in the println ??????
Stephen C
@Stephen: Of course I did, and that has no consequence if you switch the position of the println line with the line that assigns to arrayRandomLetter. They're both within the loop still. You clearly don't get what I'm suggesting, but read my comments and give it another shot. You'd be printing `(char)0 + " "` 8 times as it is now, and `(char)0 + " "` 8 times if you switched the position of those two lines.
Mark Peters
A: 

I'm not going to give you the answer, but I will give you a hint:

(char)('A' + 1) is 'B'

@fastcodejava's answer explains why you are seeing "boxes" -- rendering the ASCII NUL character.

@Mark Peters is also correct, but that's not the simplest solution.

Stephen C