-3

I am trying to make an array of random numbers that range from 0-9 with the variable for this one being kickerNumbers. I get a compile error though saying incompatible types. I have tried changing the (int) to [int] like it said but I didn't think that would be right anyway and surly enough it wasn't. Is there another way to write this? I need it to act as the first four numbers of this to be 0-9 but the fifth number would need to be something smaller such as 0-4. for the 0-4 one I just made that a completely different variable. Was that a good choice? Here is the sample code for the lines that give the error.

import java.util.Scanner;
import java.util.InputMismatchException;
public class CashBallTest
{
    public static void main(String[]args)
    {
        Scanner keyboard = new Scanner(System.in);
        int kicker;
        int[] kickerNumbers = (int)(Math.random()*0+9);
        int kickerPowerball=(int)(Math.random()*0+4);

The error reads:

\CashBallTest.java:9: incompatible types
    found   : int
    required: int[]
    int[] kickerNumbers = (int)(Math.random()*0+9);

I have also tried changing (int) to (int[]) but then it said it was an inconvertible type counting as a double when it needs to be an int[] but I don't see where it gets the double type from.

6 Answers 6

7
int[] kickerNumbers = (int)(Math.random()*0+9);

You can't assign an int to an int[]

Assuming that is supposed to be more the one number, it might be something like this

int[] kickerNumbers = new int[5];
for(int i = 0; i < kickerNumbers.length; i++) {
    kickerNumbers[i] = (int)(Math.random()*0+9);
}

On an unrelated note, why are you multiplying the Math.random()*0? That seems silly to me. If you want a number 1 through 10, I would do this: Math.random()*10 + 1

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for pointing out the random() issue. (and posting before me :) )
4
  1. This is an int array: int[] kickerNumbers but you assign it with int (not array) (int)(Math.random()*0+9);
  2. your random will return 9. always (operator precedence).

Create an array like this: int[] kickerNumbers = int[numberOfPlayers] and assign numbers in loop, or use ArrayList with numbers from 0-9 and call shuffle

Comments

4

Simply:

java.util.Random r = new java.util.Random();
int[] kickerNumbers = r.ints(0, 10).limit(5).toArray(); // 5 times 0...9

Then change last element:

kickerNumbers[4] = r.nextInt(5);

Comments

3

It may be work using your own random number generator so you can use nextInt and avoid some confusion.

Random r = new Random();
// four values [0, 9]
int[] kickerNumbers={r.nextInt(10), r.nextInt(10), r.nextInt(10), r.nextInt(10)};
// one value [0, 4]
int kickerPowerball = r.nextInt(5);

2 Comments

+1 for r.nextInt(). better than the games with Math.random()
And faster, but that doesn't matter here. ;)
0

You're attempting to assign an int to an array which is what is giving you the error.

what you want to do it use a loop to iterate through your array and assign random values to it, you mentioned that you wanted your numbers to be between 0-9 and 0-4 the following code will give you that result:

public static void main(String[] args) {
    int[] kickerNumbers = new int[4];
    for (int i = 0; i < kickerNumbers.length; i++) {
        kickerNumbers[i] = randomRange(0,9);
    }
    int kickerPowerball = randomRange(0,4);
}
public static int randomRange(int min, int max) {
    return (int) Math.round(((Math.random() * (max - min)) + min));
}

the code above includes a method that will return a integer between to given parameters.

Comments

0
import java.util.Random;
public class CashBallTest {

    public static void main (String[]args){

        int[] kickerNumbers= new int[5];
        for (int i=0;i<4;i++)
            kickerNumbers[i]=randomize(9);
            kickerNumbers[4]=randomize(4);

        for (int i:kickerNumbers)
            System.out.println(i);
    }

    public static int  randomize(int k){
        Random rand=new Random();
        return rand.nextInt(k);
    }
}

Assuming you don't care for duplicates.

Sorry. I had not read answer from Peter... It is just the same.

1 Comment

You also should not get a new Random() every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.