0

I want to create a program in Java, which -- basing on user inputs -- finds the area of the entered shape. But I failed to achieve that.

This is my script:

import java.util.Scanner;

public class area {
static Scanner advance = new Scanner(System.in);
public  void main(String[] args) {

    nu();
}
int length;
    int height;
int area;


public  void nu(){
    String message = advance.nextLine();

    if (message.equalsIgnoreCase("rectangle")){

        System.out.println("Enter the length of the rectangle: ");

        length  = advance.nextInt();

        //length declared.//
        System.out.println("Enter the height of the rectangle");

        height = advance.nextInt();

        //Height has been declared.//

        area = length * height;

        System.out.print("The area is: " + area);
    }
}
}

First problem is, that this code is not running and so I don't know if its working. All the other things are fine. Can you tell me, what I'm doing wrong?

trejder
  • 17,148
  • 27
  • 124
  • 216
  • Put `static` back in `main` and `nu` declaration :D – gkiko Dec 16 '14 at 11:19
  • As you are a new user here, let me tell you, you should accept an answer here if you get the solution of your problem. It helps others to know what worked for you. To accept an answer just click on the Right Tick sign on the left side of answer which you want to select. – gprathour Dec 16 '14 at 12:02

3 Answers3

1

You need to add static to the main method and create a new instance of area. See the code below.

public class area {
static Scanner advance = new Scanner(System.in);
public static void main(String[] args) {

    new area().nu();
}
int length;
int height;
int area;


public  void nu(){
    String message = advance.nextLine();

    if (message.equalsIgnoreCase("rectangle")){

        System.out.println("Enter the length of the rectangle: ");

        length  = advance.nextInt();

        //length declared.//
        System.out.println("Enter the height of the rectangle");

        height = advance.nextInt();

        //Height has been declared.//

        area = length * height;

        System.out.print("The area is: " + area);
    }
  }
}
James Fox
  • 691
  • 7
  • 24
1
  1. You need to add static to the main method of your program.
  2. You won't be able to directly call the nu() method from the main, as it is instance method to call it you will need to create an object of your class.

    public static void main(String[] args) {
    
        area a = new area();
        a.nu();
    }
    
  3. The other alternate is that you can make your nu() method to be static but then you will not be able to use int length;int height;int area; instance variables directly in your static method nu(). Then you will either need to make these variables to be static too or create an object of the class and then use these variables as per your need.

gprathour
  • 14,813
  • 5
  • 66
  • 90
0

Because of Java program can't find the main method to run: We need to add static keyword in 3 places according to your code.

Please try with below full source code:

import java.util.Scanner;

public class area {
    static Scanner advance = new Scanner(System.in);

    public  static void main(String[] args) { //1st change

        nu();
    }
    static int length; //2nd change
    static int height; //2nd change
    static int area; //2nd change

    public static  void nu(){ //3rd change
        String message = advance.nextLine();

        if (message.equalsIgnoreCase("rectangle")){

            System.out.println("Enter the length of the rectangle: ");

            length  = advance.nextInt();

            //length declared.//
            System.out.println("Enter the height of the rectangle");

            height = advance.nextInt();

            //Height has been declared.//

            area = length * height;

            System.out.print("The area is: " + area);
        }
    }
}
Ye Win
  • 2,020
  • 14
  • 21