tags:

views:

256

answers:

6

When i try to compile this:

import java.awt.* ;

    class obj
    {
        public static void printPoint (Point p) 
        { 
            System.out.println ("(" + p.x + ", " + p.y + ")"); 
        }
        public static void main (String[]arg)
        {
            Point blank = new Point (3,4) ; 
            System.out.println (printPoint (blank)) ;
        }
    }

i get this error:

obj.java:12: 'void' type not allowed here
        System.out.println (printPoint (blank)) ; 
                                               ^
1 error

I don't really know how to start asking about this other than to ask:

  • What went wrong here?
  • What does this error message mean?
+5  A: 

You are trying to print the result of printPoint which doesn't return anything. You will need to change your code to do either of these two things:

class obj
{
    public static void printPoint (Point p) 
    { 
        System.out.println ("(" + p.x + ", " + p.y + ")"); 
    }
    public static void main (String[]arg)
    {
        Point blank = new Point (3,4) ; 
        printPoint (blank) ;
    }
}

or this:

class obj
{
    public static String printPoint (Point p) 
    { 
        return "(" + p.x + ", " + p.y + ")"; 
    }
    public static void main (String[]arg)
    {
        Point blank = new Point (3,4) ; 
        System.out.println (printPoint (blank)) ;
    }
}
Andrew Hare
+1  A: 

You are passing the result of printPoint() - which is void - to the println() function.

Carl Manaster
+3  A: 

If a method returns void then there is nothing to print, hence this error message. Since printPoint already prints data to the console, you should just call it directly:

printPoint (blank); 
Justin Ethier
+2  A: 

The type problem is that println takes a String to print, but instead of a string, you're calling the printPoint method which is returning void.

You can just call printPoint(blank); in your main function and leave it at that.

quixoto
A: 

printPoint prints by itself rather than returning a string. To fix that call printPoint (blank) without the System.out.println.

A better alternative may be: make printPoint(Point p) return a string (and change its name to something like FormatPoint), that way the method may be used to format a point for the console, gui, print, etc rather han being tied to the console.

ggf31416
A: 

You probably wanted to do : printPoint (blank);. Looks like you are trying to print twice; once inside printPoint() and once inside main().

fastcodejava