Chris
Chris

Reputation: 193

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.

Upvotes: 4

Views: 143104

Answers (4)

duffymo
duffymo

Reputation: 308763

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.

Upvotes: 2

akatkinson
akatkinson

Reputation: 548

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);

Upvotes: 0

Edmund
Edmund

Reputation: 10819

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.

Upvotes: 2

Kirk Woll
Kirk Woll

Reputation: 77546

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)

Upvotes: 12

Related Questions