# Thread: Java program problem.. Arrays.. Random Numbers

1. Member
Join Date
Nov 2009
Posts
75
Rep Power
0

## Java program problem.. Arrays.. Random Numbers

Hey guys and girls, i have my array program up and running fine (which is a random number generator 1-999) but im finding the last bit of my program quite difficult, im new to java and if an experienced java programmer could give me some advice or hints then i would appreciate it greatly. what i need to do is :-

so i need 10 rows of 10 random numbers
and numbers below 100 0 before the number so 090
and numbers less than 10 00's before it so 009

import java.util.Random;
public class MyRandom
{
public static void main(String[] args)
{
int[] rannum = new int[100];
int rows = 10;

for(int i = 0; i < rannum.length; i++)
{
rannum[i] = new Random().nextInt(999);
}

for(int i: rannum)System.out.print(i + ",");

}
}

2. Use a DecimalFormat object. For e.g.,
Java Code:
```import java.text.DecimalFormat;

public class Fu1 {
public static void main(String[] args) {
DecimalFormat dFormat = new DecimalFormat("000");

for (int i = 0; i < 10; i++) {
for (int j = 1; j <= 10; j++) {
int value = j + i * 10;
System.out.print(dFormat.format(value) + " ");
}
System.out.println();
}
}
}```

3. The other option is to use printf with the "%03d" option:
Java Code:
```        //System.out.print(dFormat.format(value) + " ");
System.out.printf("%03d ", value);```

4. Member
Join Date
Nov 2009
Posts
75
Rep Power
0
hey thanks alot for the feedback. i have been working on my programmer i have ammended it slightly. how could i possible enter an array into my program? would appreciate the help to be honest...

import java.util.Random;
public class StuRandom {

public static void main(String[] args) {

Random rand = new Random();

for (int c = 0; c < 10; c++) {

System.out.println();

for (int i = 0; i < 10; i++) {

int j = rand.nextInt(100);

int l = String.valueOf(j).length();

if (l == 1) {

System.out.print("00" + (j) + " ");

}

else {

System.out.print("0" + (j) + " ");

}

}

}

}

}

5. 1) Please use code tags when posting code
2) You've just shown that you've ignored both of my recommendations above. Why?

6. Member
Join Date
Nov 2009
Posts
75
Rep Power
0
sorry about that i am listening, i need to add an array to generate the program thats the only reason why i didnt reply sorry bout tht

7. 1) Clarify what you mean by "adding an array", since by itself this can mean a million things, and
2) Edit your posts above to use code tags.

8. Member
Join Date
Nov 2009
Posts
75
Rep Power
0
many thanks for the speedy replys to my posts what i mean by array is as follows my goal is to Write a program called Random100 that fills an array with one hundred random values between 0 and 999. However, before a random value is entered into the array the program must check that the number is not already in the array. If it is a new random number must be generated and similarly checked before entry. This process is repeated until a suitable number has been generated.

Java Code:
```import java.util.Random;
public class StuRandom {

public static void main(String[] args) {

Random rand = new Random();

for (int c = 0; c < 10; c++) {

System.out.println();

for (int i = 0; i < 10; i++) {

int j = rand.nextInt(100);

int l = String.valueOf(j).length();

if (l == 1) {

System.out.print("00" + (j) + " ");

}

if (l == 2) {
System.out.print("0" + (j) + " ");
}

if (l == 3) {
System.out.print((j) + " ");
}

}

}
}
}```
Last edited by Chewart; 11-16-2009 at 10:42 PM. Reason: made ammendments to my program

9. Please note the difference between the forward slash and the back slash when creating code tags. Please do not repost your code, but edit your post(s) above to fix this.

As to adding the array, what have you tried so far? Let's see your best effort at this, as it will help us know what needs to be fixed. Also, you're still not using printf or DecimalFormat which are more elegant solutions to your number formatting than what you have above.

10. Member
Join Date
Nov 2009
Posts
75
Rep Power
0
honestly i really appreciate your advice on this subject. im a beginner programmer, i have only been doing thsi for two months. So most of the java language is new to me lol

11. Also, has your class gotten into creating and using methods yet? Because you're going to likely want to create one for getting a new random number. Inside this method, you'll iterate through the array to see if the number has been used before, and if not, add it to the array.

12. Member
Join Date
Nov 2009
Posts
75
Rep Power
0
we have just started on that subject today fubarable

13. Member
Join Date
Nov 2009
Posts
75
Rep Power
0
could you possiblre send me in the right direction with some sample data so i can work off that please?

14. pseudocode
Java Code:
```method add random number returns an int

do while (true)
get random number
set arrayHasNumber to false
for loop to iterate through array
if number found in array, set arrayHasNumber to true
end for loop

if arrayHasNumber still false then return number
end while loop
end method```

15. Member
Join Date
Nov 2009
Posts
75
Rep Power
0
i know this is going to sound really really dumb now dude. but where do i declare my array? i have done array programs for the past week or so and no major problems its just with this random number generator that im finding it quite difficult to grasp

16. If you are doing this all with static methods, I'd declare the array in the class as a static variable.

Java Code:
```public class StuRandom {
private static int[] intArray = new int[100];

//.... other stuff

}```
This way, any static method, including main can use the same array variable.

17. Member
Join Date
Nov 2009
Posts
75
Rep Power
0
Hey Fubarable many thanks on the guidence and help throughout my post, really do appreciate it.. i have run my program after entering the array element into it,it seems to be working fine. was wondering what do you think of my layout? and where as to commenting my program how many is too much comments?

Java Code:
```import java.util.Random;

public class StuRandom
{

public static void main(String[] args)
{
int[] intArray = new int[100];
Random rand = new Random();

for (int c = 0; c < 10; c++)
{
System.out.println();

for (int i = 0; i < 10; i++)
{
int j = rand.nextInt(999);
int l = String.valueOf(j).length();

if (l == 1)
{
System.out.print("00" + (j) + " ");
}
if (l == 2)
{
System.out.print("0" + (j) + " ");
}
if (l == 3) {
System.out.print((j) + " ");
}

}

}
}

}```

#### Posting Permissions

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