0

It says that public user number is an invalid method. It states that the part of the code is invalid and requires a return type.

  public UserNumber(){
    super();
    randy = new java.util.Random();

} //=======================

What do I do to fix it?

7 Answers7

2

In Java method has to have return type (int, Integer, Random) or void if it is not intended to return anything. Add void after public.

public void UserNumber() {
    ...
}

There is a chance you wanted to create a class constructor, but then its name has to be the same as the class name. For example:

public class UserNumber {
    private final Random randy;

    public UserNumber() {
        super();
        randy = new Random();
    }
}

(in that case calling super() can be omitted - it is performed anyway)

Please provide full code of this class.

Marcin Zajączkowski
  • 4,036
  • 1
  • 32
  • 41
1

Constructor name should be identical as your class name!

public class UserNumber {
     public UserNumber() {
        super();
        randy = new java.util.Random();
    } 
}

To invoke this special method use: new UserNumber() - this is all :)

More:

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155
1

Unless you class is called UserNumber, you need to add a return type to the method declaration:

public void UserNumber(){
    randy = new java.util.Random();
}

void as return type, since that method won't return anything.

If you, instead, wanted to make it a constructor, the name of the constructor must be the same of the class.

class UserNumber extends ...{
    public UserNumber(){
        super();
        randy = new java.util.Random();
    }
    ...
}
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

Add void after public. Every method needs a return type and this will set it to return nothing.

Marenthyu
  • 165
  • 1
  • 12
0

You are missing the return type. It must be Whether void, int or any object type.

neoprez
  • 349
  • 2
  • 11
0

The call to super() makes me think that this is supposed to be a constructor. Your class must be called UserNumber

In this case you do not need a return type, since constructors always return a new instance of the class.

If it's not a constructor, get rid of the call to super(), as you may only call that in a constructor. Also make sure that the class you're extending has a parameterless constructor.

SirRichie
  • 1,176
  • 9
  • 14
0

I think1 that the real problem is that you are confused over the difference between a method and a constructor, and you are trying to use a constructor as if it was a method.

Your UserNumber so-called method is actually declared (properly!) as a constructor1. The way to use is as a constructor is as follows:

  UserNumber myNum = new UserNumber();

I suspect you are trying to call it like:

  ... = UserNumber();

... and that is going to fail, saying that UserNumber is an invalid method. A Java constructor is invoked using new.


There are 3 clues that say to me that the OP's code is intended to be a constructor:

  1. The super() syntax is only valid in a constructor.
  2. A method declaration requires a return type AND a method name. This has only one identifier before the parameter list.
  3. You've used an identifier that should be a class name (according to the Java identifier rules).

1 - I can't be sure, because you didn't show us the line with the syntax error on it. Or if you did, then the real problem is somewhere else ... in code that you haven't shown us.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216