Results 1 to 17 of 17
  1. #1
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Exclamation 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #4
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    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. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

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

  6. #6
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    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. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #8
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    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

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #10
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    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. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #12
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    we have just started on that subject today fubarable

  13. #13
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    could you possiblre send me in the right direction with some sample data so i can work off that please?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #15
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    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. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #17
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    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

  1. Random numbers and arrays
    By caro in forum New To Java
    Replies: 6
    Last Post: 06-10-2009, 01:09 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 02:04 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. random numbers without using java funtions
    By carlos123 in forum New To Java
    Replies: 8
    Last Post: 11-16-2007, 10:13 AM

Posting Permissions

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