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, but there are no errors there, so I'm not gonna include it. Anyway, the line that says "lotteryNumbers[counter] = nextInt(int 10);" get a ".class expected" error.

0

4 Answers

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

nextInt(int 10);

Should be:

nextInt(10);

This is assuming, of course, that you actually have a method nextInt defined. (I don't see it in your code sample)

Java's an object-oriented language. What object are you invoking nextInt(10) on? I don't see one. The compiler will assume this implicitly. Does your Lottery use a Random instance somewhere? I don't see it.

I think you need 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 issues with what you're doing:

  • Unnecessary "magic" numbers everywhere. It's possible to make this class far more flexible than what you've got.
  • Mixing input into classes like this is a bad idea. Make an abstraction that you can pass values into and leave where you get them from alone. Think "single responsibility".
  • I don't see why Lottery needs a private data member for user numbers. However, I can see where it might have a method that would accept user numbers and tell whether they won or not. You've created a poor abstraction, in my opinion.

This might get you going for a while.

4

What's the int for?

If you're trying to cast, it should be (int).

The reason you're getting that error is that when Java sees a type name where an expression is expected, it thinks you're trying to refer to that type's class object, e.g. int.class.

Without knowing the specifics of nextInt(), I'd suggest the error would be from the 'int' keyword before the parameter you're passing to it. Try:

lotteryNumbers[counter] = nextInt(10);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest