Link to home
Start Free TrialLog in
Avatar of suekathur
suekathur

asked on

JAVA CODE CLARIFICATION

Here is my script that works.  I need to have the numbers, when generated, not be duplicated.  I understand that I should use an array, but I do not know how.  Some clarification would be appreciated.  Thank you.

import java.util.Random;
public class Math
{
   public static void main ( String args[] )
   {
      Random randomNumbers = new Random();
      int number;
      for ( int counter = 1; counter <= 30; counter++)
      {
         number = 1 + randomNumbers.nextInt( 49 );
         System.out.printf("%d   ", number);
         if ( counter % 6 == 0 )
            System.out.println();
      }
   }
}
ASKER CERTIFIED SOLUTION
Avatar of sh0e
sh0e

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of suekathur
suekathur

ASKER

The way I read the code is that you want to use a boolean stating that oldnums will equal a new integer and if the old number equals the same number then continue with the random number selection.  Am I reading this right?
I also tried running the new code and it does not work so I know things are missing.  Can you point me in the right direction, please.  Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When running the code I get the following error:
incompatible types
found: int[]
required: boolean[]
boolean[] oldnums = new int[50];
I know that the boolean will need to be changed to a string and I have looked on the internet for possible solutions but I do not understand their reasoning.  I found this:  String myString = Integer.toString(my int value).









SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Receiving the error: variable number might not have been initialized
while(oldnums[number]){number = 1 + randomNumbers.nextInt( 49 );}
the error happens under the [number] in the code.
Since the number has been intialized as an int number in the above code I do not know whyI am getting this.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This still does not work.  I am going to try a different method - maybe two arrays instead of one.
Consider this closed.
Thanks for your help.
Thank you for your help.  This is getting too complicated for me to understand so I am going to try something else.  It was suggested utilizing two arrays instead of one so I will try that,