Java ".class expected"

import java.util.Random;
import java.util.Scanner;

public class Lottery
{
    private int[] lotteryNumbers = new int[5];
    private int counter;
    private int[] userNumbers = new int[5];
    private Scanner keyboard = new Scanner(System.in);  
    public Lottery()
    {
        for(counter = 0; counter < 5; counter++)
        {
            lotteryNumbers[counter] = nextInt(int 10);
        }
    }

      

There is more code after the code, but there are no errors there, so I'm not going to include it. Anyway, the line that says "lotteryNumbers [counter] = nextInt (int 10);" get the error ".class expected".

+3


source to share


4 answers


Java already knows the type of the method parameter; you don't need to specify it when calling the method.

nextInt(int 10);

      

Should be:



nextInt(10);

      

This assumes, of course, that you have a specific method nextInt

. (I don't see it in your example code)

+7


source


Java is an object oriented language. Which object are you referring to nextInt(10)

? I do not see him. The compiler will assume this implicitly. Does your Lottery

instance use it Random

somewhere? I don't see that.

I think you want something like this:

private Random random = new Random(System.currentTimeMillis());

      

Then your loop should do this:



lotteryNumbers[counter] = this.random.nextInt(10);

      

I have other problems with what you are doing:

  • Unnecessary "magic" numbers all over the world. This allows this class to be much more flexible than what you have.
  • Mixing input into classes like this is a bad idea. Do an abstraction so you can pass in values ​​and leave them when you get them. Think of "single responsibility".
  • I don't understand why Lottery

    a private data member is needed for custom numbers. However, I can see where it might have a method that will take user numbers and determine if they won or not. In my opinion, you created a bad abstraction.

This may take some time.

+2


source


What is int

for?

If you are trying to throw, it should be (int)

.

The reason you are getting this error is because when Java sees the name of the type in which the expression is expected, it thinks you are trying to access that class of a class, for example. int.class

...

+1


source


Without knowing the specifics of nextInt (), I would assume the error would be due to the 'int' keyword in front of the parameter you pass to it. Try:

lotteryNumbers[counter] = nextInt(10);

      

0


source







All Articles