CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Error help on println

    Hi, I started studying java on my own a few days back. Anyway, can plase someone tell me what is wrong with this piece of code?:

    Code:
    public class bananas {
    	
    	int width =2 , height = 5;
    	int area = getArea() ;
    	System.out.println("The area is " + area);
    	int getArea (){
    		return (width * height);
    	}
    
    }
    Eclipse says - Syntax error on token "println", = expected after
    this token
    - Syntax error on token(s), misplaced construct(s)


    Thanks for the help

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Error help on println

    The println call is in class scope (i.e. in the class body) instead of in method scope (i.e. inside a method). In general, you can only put variable and method declarations in the class body. Other code must go inside methods.

    Incidentally, the Java naming convention is for class names to start with an uppercase letter and method & variable names to start with a lowercase letter.

    Programs must be written for people to read, and only incidentally for machines to execute...
    H. Abelson and G. Sussman
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Apr 2010
    Posts
    3

    Re: Error help on println

    So I have to put the println inside braces? I tried to do that and the error did go away, but when I try to run the program (like it is presented below) it gives another error: java.lang.NoSuchMethodError: main
    Exception in thread "main"

    Code:
    public class bananas {
    	
    	int width =2 , height = 5;
    	int area = getArea() ;
    	{
    		System.out.println("The area is " + area);
    		}
    	int getArea (){
    		return (width * height);
    		}
    }

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Error help on println

    Quote Originally Posted by JMaia View Post
    So I have to put the println inside braces?
    No, it must go inside a method, like all executable code.

    when I try to run the program (like it is presented below) it gives another error: java.lang.NoSuchMethodError: main
    That's because to execute a class, the Java runtime looks for a method called 'main'. The 'main' method is known as the 'entry point' because it tells the Java runtime where to start executing the code.

    This is really basic Java knowledge. I strongly recommend that you take a Java course, read a Java book, or take an online tutorial - at least. Most of us are here to help people solve their Java problems, not to teach Java - there are much better & quicker ways of learning Java than a forum.

    If you get stuck on something, or don't understand something, you're welcome to ask.

    If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Error help on println

    If you want a class to be executable you need to declare a main method, which is the entry point for the application. The main method is declared as follows:

    Code:
    public static void main(String[] args)
        {
        ... Your Code Here ...
        }
    Where 'args' is the command line arguments, if any are provided.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Apr 2010
    Posts
    3

    Re: Error help on println

    Thanks for the help I think I got it now.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured