Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple Issue I Cant Solve

    Can someone explain why this wont compile. Its asking for a "}" but there is no need for one that I can find. Super simple issue thats so frustrating.


    Code:
    //package Dice;
    import java.util.Random;
    
    public class Die {
    	private final Random random = new Random();
    	public int max;
    	public int result;
    	
    	public Die(int dieMax) {
    		max = dieMax;
    	}
    
    
    	public int roll(max) {
    		result = 1 + random.nextInt(max);
    		System.out.println(result);
    		return result;
    	}
    }
    Last edited by vinyl-junkie; Sep 18th, 2018 at 08:17 PM. Reason: corrected code tags

  2. #2
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,190
    Thanks
    4
    Thanked 463 Times in 451 Posts
    Can an object instance BE final? Hmm, that wouldn't cause the error in question. Though I've got a decade and a half of ring rust when it comes to Java, who knows how far off the mark I might be.. Should you be saying public on the constructor? I thought that was assumed? (since without the constructor public you could never "new" the class!)

    Tried it here, and the error I get is "identifier expected" -- which would be for this line:

    public int roll(max) {

    Which makes sense since there's no type declared for 'max'... NOT that I think you even want to be setting 'max' there since you declared it in the constructor. You swap that for:

    public int roll() {

    and it compiles fine for me; and makes sense as then it will use the max set in Die()
    Last edited by deathshadow; Sep 18th, 2018 at 08:55 PM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  3. #3
    New Coder
    Join Date
    Jan 2016
    Location
    Malaysia
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    An argument in a function must have datatype.
    Code:
    public int roll(int max) {


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •