Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Need Help with Array's

  1. #1
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Need Help with Array's

    Hey I'm learning java for my major and I'm doing a project for class but I'm Struggling with getting random numbers added to an array and comparing the elements of the array with a while and nested for loop. One is suppose to be to enter the numbers while the other is suppose to compare the elements in the array.
    At least that's the hint our teacher gave to us. I've posted the relevant code at the bottom. I'm getting an error at the digits part that it is not a statement.

    PHP Code:
    public class Number {
          private int[] digits;
       
          public Number(int n) {
             Random rand = new Random();
             int[] digits = new int[n];
             int i = 0;
             while (i < digits.legnth) {
                 rand.nextInt(10);
                for(int a = 0; a < digits.length; a++) {
                   digits[] = rand.nextInt(10); 
                	
                   
                      
                }
             }
          }

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

    Default Re: Need Help with Array's

    Quote Originally Posted by Quicksilver2011 View Post
    Hey I'm learning java for my major and I'm doing a project for class but I'm Struggling with getting random numbers added to an array and comparing the elements of the array with a while and nested for loop. One is suppose to be to enter the numbers while the other is suppose to compare the elements in the array.
    At least that's the hint our teacher gave to us. I've posted the relevant code at the bottom. I'm getting an error at the digits part that it is not a statement.

    PHP Code:
    public class Number {
          private int[] digits;
       
          public Number(int n) {
             Random rand = new Random();
             int[] digits = new int[n];
             int i = 0;
             while (i < digits.legnth) {
                 rand.nextInt(10);
                for(int a = 0; a < digits.length; a++) {
                   digits[] = rand.nextInt(10); 
                	
                   
                      
                }
             }
          }
    I'm unclear on your actual instructions, can you post them verbatim? Are you forced to use while loops and not for loops?

    Regarding this:
    I'm getting an error at the digits part that it is not a statement.
    If you have any error messages, please post the actual and entire error message. They usually say exactly what is wrong.

  3. #3
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need Help with Array's

    Number.java:20: error: not a statement
    digitsIn[] = rand.nextInt(10);
    ^
    Number.java:20: error: ';' expected
    digitsIn[] = rand.nextInt(10);
    ^

    Those are the errors I received.
    And Here are my instructions

    Constructor: Accepts an int n parameter representing the number of digits entered by the
    user and generates n different random numbers between 0 and 9 (inclusive) and assigns them
    to the elements of the int array. Note that “Different” means no repeating numbers.

    This was the hint that was given
    Code and Test: The most complicated method in the Numbers class is the constructor. See the
    Java API documentation (Math class or Random class) for details on how to generate a random
    number. Make sure that the integers generated are random numbers between 0 and 9 (inclusive)
    and that they do not repeat (i.e., they are different from each other). One approach would be to
    have an outer while loop which contains a for loop to generate the random digits followed by
    nested for loops to check for duplicates. The outer while should loop until an acceptable set of
    random digits has been generated.

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

    Default Re: Need Help with Array's

    Quote Originally Posted by Quicksilver2011 View Post
    Number.java:20: error: not a statement
    digitsIn[] = rand.nextInt(10);
    You can only use empty square brackets when declaring an array. When assigning values in the array, you need to use indices. Please read your text or tutorial on how to use arrays to see more on this.


    Java Code:
    Number.java:20: error: ';' expected
                   digitsIn[] = rand.nextInt(10); 
                             ^
    This is likely a direct result of the first error.

    This was the hint that was given
    Code and Test: The most complicated method in the Numbers class is the constructor. See the
    Java API documentation (Math class or Random class) for details on how to generate a random
    number. Make sure that the integers generated are random numbers between 0 and 9 (inclusive)
    and that they do not repeat (i.e., they are different from each other). One approach would be to
    have an outer while loop which contains a for loop to generate the random digits followed by
    nested for loops to check for duplicates. The outer while should loop until an acceptable set of
    random digits has been generated.
    Maybe it's me, but I'm just not getting the "outer while loop" that he recommends. If this were my project, I'd use an outer for loop, and inside of this use a while loop that kept repeating until the random number selected was found to be unique. It would be:

    Java Code:
    for loop with int index that goes from 0 to < length of array
       declare a boolean, unique
       repeat 
          unique is set to true.
          get random number
          for loop through previous numbers to see if current number is unique.
              if match found, unique is set to false.
       until random number is unique 
    end for index goes from...

  5. #5
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need Help with Array's

    How would you compare elements in the array that's one thing although I've been searching I'm still not sure how to implement?

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

    Default Re: Need Help with Array's

    Quote Originally Posted by Quicksilver2011 View Post
    How would you compare elements in the array that's one thing although I've been searching I'm still not sure how to implement?
    You would use an inner for loop that loops from 0 to one minus the current index of the outer for loop.

  7. #7
    lifedistroy is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need Help with Array's

    I think i can help you out here, There is a simple brute force check that checks all current numbers in a whileloop

    int CURRENTNUMBER=( your last array place +1)
    int counter=1;
    int W=0;
    While(counter>0){
    if(digits[W]=digits[CURRENTNUMBER]){
    //Code to rerun your number generator
    }else{
    W++;
    counter++;
    }
    counter--;

    }
    Last edited by lifedistroy; 03-20-2012 at 05:04 AM.

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

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 12:32 PM
  2. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 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
  •