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();
}
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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).
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Receiving the error: variable number might not have been initialized
while(oldnums[number]){num ber = 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.
while(oldnums[number]){num
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Consider this closed.
Thanks for your help.
ASKER
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,
ASKER
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