What causes "'void' type not allowed here" error
Solution 1:
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);
Solution 2:
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)) ;
}
}
Solution 3:
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.