6 Replies - 3314 Views - Last Post: 06 July 2012 - 09:08 AM Rate Topic: -----

#1 Wendyb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-July 12

compiling error - Class, interface, or enum expected

Posted 06 July 2012 - 08:41 AM

I know this is so very basic, but I am just trying to learn java. Can someone please give me a pointer on what the problem is with the following code where I am getting the error "class, interface, or enum expected". I just can't see the problem (this is a scenario out of Greenfoot that I'm changing).

import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * This class defines a crab. Crabs live on the beach. They like sand worms 
 * (very yummy, especially the green ones).
 * 
 * Version: 2
 * 
 * In this version, the crab walks around the beach more or less randomly.
 */


public class Crab extends Animal
{
    /** 
     * Act - do whatever the crab wants to do. This method is called whenever
     *  the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move();
        turnAtEdge();
        lookForWorm();
        randomTurn();
       
        }
    }
/**
 * Check whether we have stumbled upon a worm.
 * If we have, eat it. If not, do nothing.
 */
public void lookForWorm()      This is where the error is 
{ 
    if (canSee(Worm.class))
    {
        eat(Worm.class);
    }

}
/**
 * Makes us turn left or right randomly
 */
  public void randomTurn()
{
    if (Greenfoot.getRandomNumber(100) < 10) 
        {
            turn( Greenfoot.getRandomNumber(90)-45);
        }
}

/**
 * Makes us turn at the edge of our world
 */
public void turnAtEdge()
{
    if (atWorldEdge() ) 
        {
            turn(17);
        }
}


This post has been edited by macosxnerd101: 06 July 2012 - 08:42 AM
Reason for edit:: Post your code between the code tags


Is This A Good Question/Topic? 0
  • +

Replies To: compiling error - Class, interface, or enum expected

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: compiling error - Class, interface, or enum expected

Posted 06 July 2012 - 08:42 AM

Curly brace in the wrong place (should be one extra at the very end of the file

public void act()
{
move();
turnAtEdge();
lookForWorm();
randomTurn();

}
}
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: compiling error - Class, interface, or enum expected

Posted 06 July 2012 - 08:43 AM

Match up your curly braces. Notice how you close the Crab class following the closing brace for the act() method.
Was This Post Helpful? 1
  • +
  • -

#4 Wendyb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-July 12

Re: compiling error - Class, interface, or enum expected

Posted 06 July 2012 - 08:59 AM

Thank you for entering my code correctly, I'll try and do it right next time.

I still can't see where my curly braces are not lined up. Is that what the problem is?

Of course you were right!!! I just found it. For as long as I have looked at that, I will not make that mistake again...I hope.

THANK YOU!
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7630
  • View blog
  • Posts: 12,866
  • Joined: 19-March 11

Re: compiling error - Class, interface, or enum expected

Posted 06 July 2012 - 09:00 AM

What this error message actually means is that the compiler was looking for a class, interface, or enum definition and found something else. It knows what to look for based on the level of nesting it's at: when nested at depth zero, inside of no curly braces, it expects package declarations, import statements, and class-like definitions (interfaces and enums are class-like). A classy definition is defined by a set of curly braces, so when we're nested one deep, we expect to find attributes of a class: fields, constructors, and methods are what you're most concerned with now. Once you're nested two deep, you have a couple of places where you could be, but if you're in a method you're expecting local variable declarations and actual java instructions (which might themselves create more nesting, and so forth).

As the boys have pointed out, if you don't get the braces matched up correctly, the compiler gets confused. The best way to ensure good brace matching is to line up your braces, in what's called Allman-style code formatting:

public class Foo 
{
  public void bar() 
  {
    if (singer.equals(gardel))
    {
      tango();
    }
    else if (singer.equals(jobim))
    {
      samba();
    }
  }

}


See how there's no way I can get lost if I line these up this way? It helps.
Was This Post Helpful? 1
  • +
  • -

#6 RCR  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Re: compiling error - Class, interface, or enum expected

Posted 06 July 2012 - 09:01 AM

Check out my Java Tutorials I'll be making!
Was This Post Helpful? 0
  • +
  • -

#7 Wendyb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-July 12

Re: compiling error - Class, interface, or enum expected

Posted 06 July 2012 - 09:08 AM

Thanks for the explanation Jon K., it certainly does help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1