Results 1 to 17 of 17
- 11-16-2009, 08:40 PM #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 + ",");
}
}
-
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(); } } }
-
The other option is to use printf with the "%03d" option:
Java Code://System.out.print(dFormat.format(value) + " "); System.out.printf("%03d ", value);
- 11-16-2009, 09:16 PM #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) + " ");
}
}
}
}
}
-
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, 09:25 PM #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
-
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, 09:33 PM #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 09:42 PM. Reason: made ammendments to my program
-
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, 09:45 PM #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
-
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, 09:46 PM #12
Member
- Join Date
- Nov 2009
- Posts
- 75
- Rep Power
- 0
we have just started on that subject today fubarable
- 11-16-2009, 09:47 PM #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?
-
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
- 11-16-2009, 10:07 PM #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
-
If you are doing this all with static methods, I'd declare the array in the class as a static variable.
This way, any static method, including main can use the same array variable.Java Code:public class StuRandom { private static int[] intArray = new int[100]; //.... other stuff }
- 11-16-2009, 10:21 PM #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) + " "); } } } } }
Similar Threads
-
Random numbers and arrays
By caro in forum New To JavaReplies: 6Last Post: 06-10-2009, 01:09 AM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Random numbers
By jithan in forum Advanced JavaReplies: 3Last Post: 06-14-2008, 02:04 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
random numbers without using java funtions
By carlos123 in forum New To JavaReplies: 8Last Post: 11-16-2007, 10:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks