简体   繁体   中英

Error in System.out.println

Is there any error in the following code? It shows cant find symbol, symbol: class out location: class System. In the log, it show a lot of errors, including

java.lang.ClassFormatError: Method "" in class Area has illegal signature "(Ljava/lang/Object;)Ljava/lang/System$out$println;"

import java.util.*;
class Area
{
double pi=3.14;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of r");
int r=sc.nextInt();
System.out.println("enter the value h");
int h=sc.nextInt();
void areaOfCircle()
   {
     double area1=pi*r*r;
     System.out.println("area of circle="+area1);
   }
void areaOfCylinder()
   {
     double area2=2*pi*r*(r+h);
     System.out.println("area of cylinder="+area2);
   }
public static void main(String args[])
   {
     Area a=new Area();
     a.areaOfCircle();
     a.areaOfCylinder();
   }
}

You can't place code outside methods in Java. You have

System.out.println("Enter the value of r");

which is not belonging to anything. Fix these issues and the problem will go away.

Just for curiosity, how should code outside methods be called and from what according to you? What I mean is that the execution is made by a code flow which starts from a entry point (the main method in Java) and jumps to methods called, eventually spawning other threads. Code which doesn't reside inside a method is not reachable nor it leads to anything.

The statement System.out.println(""); should be written in some block. It cannot be written directly in class.

public class ClassName {
   System.out.println("this statement gives error"); // Error!! 
}

Either it should be inside curly braces {...} like:

{ System.out.println("this works fine"); }

This way is an initializer block.

Or it should be written in a method like:

public void methodName(){
    System.out.println("inside a method, prints fine");
}

Your complete program should be like:

public class Area {
double pi = 3.14;
int r;
int h;

void areaOfCircle() {
    double area1 = pi * r * r;
    System.out.println("area of circle=" + area1);
}

void areaOfCylinder() {
    double area2 = 2 * pi * r * (r + h);
    System.out.println("area of cylinder=" + area2);
}

public static void main(String args[]) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the value of r");
    Area a = new Area();
    a.r = sc.nextInt();
    System.out.println("enter the value h");
    a.h = sc.nextInt();
    a.areaOfCircle();
    a.areaOfCylinder();
   }
}

In java does not work like this any behaviour you want to implement you must do it inside a block or a method

It needs to be inside an executable block of code to be executed. Otherwise there's no way to know when to execute it.

Remember that a class can only have attributes or methods.Attributes are the properties of the class and methods represent the behaviour of the class.So every implementation goes inside a method or a block.

The only things allowed outside method and constructor declarations are declarations of fields. Since

Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of r");
int r=sc.nextInt();
System.out.println("enter the value h");
int h=sc.nextInt();

is not a field declaration, it's not allowed.

The following code

System.out.println("Enter the value of r");
int r=sc.nextInt();
System.out.println("enter the value h");
int h=sc.nextInt();

must be inside a method. Not directly in the class. Classes can contain field and method declarations, but not arbitrary code.

You are missing the java basics.

Problem:

Processing statements should be in function only.

Solution:

Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of r");
int r=sc.nextInt();
System.out.println("enter the value h");
int h=sc.nextInt();

Post the above code either in function or constructor by doing changes according to your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM