Results 1 to 8 of 8
Thread: Need Help with Array's
- 03-20-2012, 02:53 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
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); } } }
-
Re: Need Help with Array's
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:If you have any error messages, please post the actual and entire error message. They usually say exactly what is wrong.I'm getting an error at the digits part that it is not a statement.
- 03-20-2012, 03:23 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
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.
-
Re: Need Help with Array's
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.
This is likely a direct result of the first error.Java Code:Number.java:20: error: ';' expected digitsIn[] = rand.nextInt(10); ^
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: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.
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...
- 03-20-2012, 03:54 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
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?
-
Re: Need Help with Array's
- 03-20-2012, 04:02 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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 04:04 AM.
-
Re: Need Help with Array's
Similar Threads
-
convert byte array into char array
By kgkamaraj in forum New To JavaReplies: 4Last Post: 09-13-2011, 11:32 AM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks