# Thread: Arrays and random number method help!

1. Member
Join Date
Feb 2012
Posts
5
Rep Power
0

## Arrays and random number method help!

I need to create a program, that organizes on an array, random numbers for a marketing company and 5 products. This company wants to do a follow up of the households for these 5 products, vacuum cleaner, toothpaste, grass seed, tv remote and motor oil.

I created a menu, to select which product will be followed up; the thing is that I want to create an array that collects all this numbers, and organize them in sets of rows of ten elements each. And check if there is a duplicate of any random number, I have this, but I'm stuck:

If someone could help me; I'd be glad!!

Java Code:
```public static void main(String[] args) {
Scanner input = new Scanner( System.in);

int choice;
int number = 0;

// TODO code application logic here

System.out.println("1 - Toothpaste");
System.out.println("2 - Vacuum Cleaner");
System.out.println("3 - Motor Oil");
System.out.println("4 - Grass Seed");
System.out.println("5 - Television remote");
choice = input.nextInt();

if (choice == 1)
{
int[][] houses = new int[38][10];
for ( int i = 0; i < houses.length; i++)
{
for (int j = 0; j < houses[j].length; j++)
{
number = (int) ( 37650 * Math.random() ) + 1;
System.out.printf("%5d%8d\n",number, i, j, houses[ i ][ j ]);
}
}
}
else if (choice == 2)
{
int[][] houses = new int[39][10];
for ( int i = 0; i < houses.length; i++)
{
for (int j = 0; j < houses[j].length; j++)
{
number = (int) ( 38135 * Math.random() ) + 1;
System.out.printf("%5d%8d\n",number, i, j, houses[ i ][ j ]);
}
}
}```
Last edited by 619snake; 02-27-2012 at 02:45 AM.

I'm stuck:

3. Member
Join Date
Feb 2012
Posts
5
Rep Power
0

## Re: Arrays and random number method help!

When running the code, it does not generate the output, I've tried many ways to create an output that shows a row with 10 elements and show no duplicates of the random numbers

4. ## Re: Arrays and random number method help!

it does not generate the output
Strange.
I see lots of printlns that should generate some output.

Are you getting compiler errors that prevent the code from executing?

Please copy the output and paste it here.

5. Member
Join Date
Feb 2012
Posts
5
Rep Power
0

## Re: Arrays and random number method help!

the output looks like this, but it is not the way it is required:
It also shows 370 elements instead of 377, I'm loosing 7 elements

1 - Toothpaste
2 - Vacuum Cleaner
3 - Motor Oil
4 - Grass Seed
5 - Television remote
1
24395 0
2835 0
23447 0
5721 0
29224 0
13012 0
32260 0
35346 0
30410 0
20617 0
35033 1
6890 1
1868 1
30838 1
27615 1
3827 1
30780 1
29822 1
10053 1
36885 1
10493 2
16405 2
4634 2
16991 2
26020 2
15969 2
32295 2
18049 2
14669 2
28166 2
6859 3
5304 3
20854 3
25151 3
10624 3
20047 3
5080 3
14791 3
4594 3
27381 3
11776 4
2292 4
10804 4
27554 4
19287 4
26293 4
29260 4
30901 4
11782 4
31457 4
6661 5
17254 5
20885 5
13980 5
29843 5
9665 5
21226 5
3602 5
25548 5
2479 5
26368 6
46 6
35760 6
12787 6
13781 6
5526 6
15837 6
14492 6
7437 6
13961 6
15063 7
36169 7
15897 7
22421 7
16037 7
36404 7
9536 7
30521 7
31115 7
24067 7
28694 8
3072 8
9776 8
28158 8
33194 8
3253 8
30185 8
29729 8
2476 8
28426 8
27405 9
2104 9
24815 9
20960 9
32524 9
28730 9
8113 9
32449 9
10222 9
5199 9
31779 10
22231 10
2895 10
17572 10
6012 10
1965 10
16929 10
23863 10
21361 10
18113 10
3690 11
22939 11
15792 11
10289 11
5928 11
25177 11
1926 11
13060 11
735 11
32955 11
30914 12
13597 12
17782 12
18683 12
2358 12
28418 12
19046 12
21756 12
10758 12
26135 12
10302 13
32457 13
2268 13
2148 13
37441 13
22252 13
28561 13
19642 13
1138 13
19472 13
27821 14
25514 14
36926 14
23069 14
22954 14
31291 14
15062 14
20920 14
35082 14
31555 14
24721 15
2206 15
3310 15
32618 15
20213 15
18918 15
2819 15
17319 15
15849 15
5199 15
24893 16
24916 16
9343 16
12342 16
26070 16
8772 16
1273 16
34371 16
12174 16
7676 16
15024 17
30601 17
22949 17
3251 17
16240 17
14660 17
7588 17
3191 17
7779 17
35506 17
31961 18
20807 18
17801 18
35571 18
1683 18
28623 18
12018 18
9639 18
15787 18
28657 18
5216 19
24052 19
16035 19
29228 19
31071 19
11926 19
17035 19
1894 19
35701 19
7952 19
27877 20
21705 20
22067 20
28217 20
14880 20
16432 20
23321 20
30701 20
12485 20
28402 20
20235 21
18801 21
8074 21
16106 21
8864 21
29753 21
12283 21
32513 21
32147 21
9459 21
30925 22
34702 22
16664 22
732 22
461 22
5622 22
4547 22
6789 22
6492 22
6536 22
16793 23
25609 23
31611 23
30657 23
34226 23
17371 23
28385 23
2846 23
31005 23
16250 23
14218 24
25680 24
14901 24
7960 24
15325 24
8789 24
16082 24
12933 24
30495 24
14013 24
34881 25
29838 25
26387 25
21832 25
33600 25
4393 25
30097 25
13135 25
24969 25
24457 25
11663 26
19296 26
19717 26
14880 26
30305 26
17235 26
26831 26
4139 26
3870 26
29936 26
21695 27
8345 27
6868 27
30626 27
14284 27
30849 27
33944 27
26723 27
7681 27
35234 27
24544 28
5690 28
16497 28
27911 28
4301 28
7485 28
22324 28
2571 28
36224 28
221 28
21467 29
33688 29
26430 29
37068 29
1838 29
1082 29
31472 29
12513 29
13474 29
9637 29
6993 30
34078 30
2257 30
24117 30
19854 30
16146 30
18170 30
18858 30
24448 30
35948 30
22719 31
30956 31
21570 31
10481 31
740 31
19328 31
35656 31
28874 31
8105 31
36375 31
21433 32
25247 32
36727 32
31430 32
21520 32
19869 32
22334 32
30721 32
2216 32
27003 32
17368 33
18134 33
33531 33
21074 33
28321 33
33618 33
32225 33
23532 33
5566 33
30348 33
26649 34
2342 34
86 34
29070 34
14865 34
31587 34
33722 34
12251 34
13415 34
28157 34
10064 35
28599 35
35109 35
34600 35
26570 35
26239 35
29401 35
13680 35
19417 35
29020 35
25890 36
28769 36
14468 36
9895 36
11922 36
12511 36
12100 36
17365 36
9323 36
1747 36

6. ## Re: Arrays and random number method help!

Did you really count all those lines?
If you added an extra value to the printout the computer would count them for you:
Create an int value out side the loop and print it before (on the same line with) the values that you are printing.
Increment the variable by one every time it is used.

Why do you expect 377 lines to print?
I'd expect 380. 38*10
Last edited by Norm; 02-27-2012 at 03:03 AM.

7. Member
Join Date
Feb 2012
Posts
5
Rep Power
0

## Re: Arrays and random number method help!

ok, the thing is that I did not count all those lines, I'd expect 377 lines, because I need to generate 1% random numbers of the quantity of households for each product. but I really don't know if it is possible to create or modify this array to make it display 10 elements per row... and eliminate any duplicates

8. ## Re: Arrays and random number method help!

With the looping it will print 380 lines.

Before you write any more code you need to make a design of what the code will do. Make a list of the steps that the program needs to do to solve your problem.
When you get the list of steps, then work on writing the code for each step.

possible to create or modify this array to make it display 10 elements per row
Don't use the "\n" newline in the printf until you have all the elements you want on a line.
You could use the println() method to go to a new line when the current line is full.

9. Member
Join Date
Feb 2012
Posts
5
Rep Power
0

## Re: Arrays and random number method help!

Thanks a lot Norm!! I got all the elements in blocks :) the only thing I need to do now is to remove any duplicate number... which seems a bit troublesome, but I think I can do it

#### Posting Permissions

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