# Java program problem.. Arrays.. Random Numbers

• 11-16-2009, 09:40 PM
Chewart
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 + ",");

}
}
• 11-16-2009, 09:58 PM
Fubarable
Use a DecimalFormat object. For e.g.,
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();     }   } }```
• 11-16-2009, 10:13 PM
Fubarable
The other option is to use printf with the "%03d" option:
Code:

```        //System.out.print(dFormat.format(value) + " ");         System.out.printf("%03d ", value);```
• 11-16-2009, 10:16 PM
Chewart
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) + " ");

}

}

}

}

}
• 11-16-2009, 10:23 PM
Fubarable
1) Please use code tags when posting code
2) You've just shown that you've ignored both of my recommendations above. Why?
• 11-16-2009, 10:25 PM
Chewart
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
• 11-16-2009, 10:29 PM
Fubarable
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.
• 11-16-2009, 10:33 PM
Chewart
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.

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) + " ");           }         }       } }   }```
• 11-16-2009, 10:39 PM
Fubarable
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.
• 11-16-2009, 10:45 PM
Chewart
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-16-2009, 10:45 PM
Fubarable
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.
• 11-16-2009, 10:46 PM
Chewart
we have just started on that subject today fubarable
• 11-16-2009, 10:47 PM
Chewart
could you possiblre send me in the right direction with some sample data so i can work off that please?
• 11-16-2009, 10:58 PM
Fubarable
pseudocode
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```
• 11-16-2009, 11:07 PM
Chewart
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
• 11-16-2009, 11:13 PM
Fubarable
If you are doing this all with static methods, I'd declare the array in the class as a static variable.

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.
• 11-16-2009, 11:21 PM
Chewart
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?

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) + " ");                         }                                                 }                 }         }         }```