Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32
  1. #21
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    just wanted to no is this what you do to ensure the there is 3 digits at all time

    Since you print the value of temp you should be able to see if it is padded with zeros correctly by running the program.

  2. #22
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    lol it wont compile rrrrrrr :mad:

    thanx for ur input by the way:D

  3. #23
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    You're missing a semicolon on line 16.

    If it still won't compile after that, give us the FULL AND COMPLETE error messages (line numbers included).

  4. #24
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Those error messages don't relate to the code you posted. (They refer to a class ArrayRandom1, the code was for ArrayRandom).

    Assuming they are somewhat similar...

    ArrayRandom1.java:26: cannot find symbol
    symbol : variable zeroStr
    location: class ArrayRandom1
    zeroStr = "000"; //string with 3 zeroes.
    ^

    "Cannot find symbol" means you have not declared the variable (or method) that you are using. In this case it is zeroStr (which the ^ is pointing at) that has not been declared.

    ArrayRandom1.java:28: r is already defined in main(java.lang.String[])
    int r = String.valueOf(array[i]).length(); // get the length of element i

    "is already defined in" means you have declared something twice. In this case r had already been declared as Random. (Incidently this is a good reason to choose appropriate variable names.)

    ArrayRandom1.java:28: array required, but int found
    int r = String.valueOf(array[i]).length(); // get the length of element i

    This one is a bit cryptic (perhaps it relates to the code you haven't posted). However in the code above array was never declared. Something else was declared as an array of ints.

    -----

    It's a good idea to compile often so these messages don't accumulate.

  5. #25
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    XML Code:
    import java.util.Random;
       
          public class ArrayRandom1
     
          {
      
          public static void main(String args[])
      
          {
     
          Random r = new Random();
      
          int temp;
     
          int arr[] = new int[50];
          
          for(int i = 0; i < 50; i++)
      
          {
     
    
          temp = r.nextInt(1000); // this will give you a random number between 0 to 999
    
          zeroStr = "000"; //string with 3 zeroes.
      
          int r = String.valueOf(array[i]).length(); // get the length of element i
                                                                                                                                       
          if (r < 3) { // for digits less than 3
      
          String temp = zeroStr.substring(0, 3 - r) + String.valueOf(array[i]); // concat the zeroes together with the digit
     
          System.out.println(temp);
     
          }
     
          arr[i] = temp; //here we know that the number is unique, so we can put it inside arr[i]
      
          }
    
          for(int i = 0; i < 50; i++)
     
          {
    
          System.out.print(arr[i] + " ");
    
          }
      
          }
      
          }

    this the code i compiled to get those errors sorry I called it ArrayRandom1

    you said about the r vairible bee declared twice so I made a new varible and I got loads of errors

  6. #26
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    XML Code:
    import java.util.Random;
    public class ArrayRandom
    {
    	public static void main(String[] args)
    	{
    		Random r = new Random();
    		int i =0;
    		int array[] = new int[50];
    		int a = 0;
    	
    	
    		while(a < array.length)
    
    			{	
    			i = r.nextInt(1000);
    			array[c]=n;
    			System.out.print(array[a] + " ");
    			a++;
    			}
    	
    		}
    }

    dam upon looking at my home work it has to be a while loop so ive rechanged it

  7. #27
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    XML Code:
    import java.util.Random;
    
    public class ArrayRandom2 {
    
        public static void main(String[] args) {
            Random r = new Random();
            int i = 0;
            int array[] = new int[50];
            int a = 0;
            int p = 0;
    
    
            while (a < array.length) {
                if (p == 5) {
                    System.out.println();
                    p = 0;
                }
    
                i = r.nextInt(1000);
    
                int f = binarySearch(array, array.length, i);
                if (f == 0) {
                    array[a] = i;
                }
    
    
    
                String zeroStr = "000"; //string with 3 zeroes.
                int r1 = String.valueOf(array[a]).length();
                if (r1 < 3) {
                    String temp = zeroStr.substring(0, 3 - r1) + String.valueOf(array[a]);
                    System.out.print(temp + " ");
                } else {
                    System.out.print(array[a] + " ");
                }
                a++;
                p++;
            }
    
        }
    does this look right ?

  8. #28
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    does this look right ?

    More importantly, does it compile? If (or when) it does you can run it lots of times to check whether you get the right number of numbers in the right range.

  9. #29
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    yes it compiles I get the random numbers but i do not get 3 digits at all time like for 1 i need 001 if you no what I mean

  10. #30
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    yes it compiles


    What about binarySearch()? I wonder (given the missing closing brace) whether you aren't posting just a piece of the code.

  11. #31
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Array of random numbers ... - Dev Shed

    Sorry, I'm out.

    Parting advice though: I posed a few questions in this thread (and possibly repeated them after the range and crossposting kurfuffle was - so I thought - sorted out). It really is frustrating (for me) and unproductive (for you) not to get a reply.

  12. #32
    AnotherUser is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by shane1987 View Post
    well cheers for your help : )
    Since I'm in your class and have the same assignment, perhaps I can explain it.

    The question asks you to use two loops, one nested within the other. Nesting just means to place one thing inside the other.

    So far, we've learnt about for loops and while loops. Although while and for loops are interchangeable, there are situations where one is more suitable then the other. A for loop is commonly used where the amount of iterations is known beforehand whereas a while loop is used where we don't know how many times we want to iterate. For more detail, see the course books (Big Java or Starting Out With Java).

    Anyway, the question also asks for some psuedo-code. If you haven't already done this, do it first. It'll help you plan out the logic for your code. Figure out which type of loop you'll use where and why. That's probably half the battle with this assignment.

    To generate random numbers, you'll need to do:
    import java.util.Random.

    After you've done that, check these links out:
    Java Tips - How to generate a random number
    Random (Java 2 Platform SE v1.4.2)

    Hope that's of some help. I'd go into more detail but if Steve were to find this post... ;)

    P.S. Don't use code you can't explain. You'll probably lose marks for doing so.

    </anon student in your class>
    Last edited by AnotherUser; 11-14-2010 at 09:40 PM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Store Random numbers into Array
    By abby0910 in forum New To Java
    Replies: 19
    Last Post: 07-12-2010, 12:59 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 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
  •