-3

I'm trying to create a program that reads user input and stores it and then calculates the area of a polygon. When I try and compile it it gives me one error which is the one about .toString being non static.

import java.util.Scanner;

class regularpoTest {
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        boolean finished = false;
        double s;
        double n;
        double area;
        //starts loop to record data
        do {
            s =0;
            n =0;
        
        
            System.out.println("Enter the side length, or anything else to quit: ");
            s = in.nextDouble();
            in.nextLine();
            if (in.hasNextDouble()) {
                System.out.println("Enter number of sides");
                n = in.nextDouble();
                area = (s*s*n)/(4*Math.tan(Math.PI/n));
            } else {
                finished = true;
            
            }
        } while(!finished);
    
        //This prints out the student details 
    
        System.out.println(regularpo.toString());
    
    }
}  

public class regularpo {

    private double side;
    private double numberOf;
    private double area;
    
    public regularpo(double side, double numberOf){
        side = 0;
        numberOf = 0;
        area = 0;
    }
    
    public double getSide(){
        return side;
    }
    public double getNumberOf(){
        return numberOf;
    }
    public String toString(){
        return ("area = " + area+ " side length "+side+ " number of sides "+numberOf);
    }
}
Jason Aller
  • 3,391
  • 28
  • 37
  • 36
Carlo Sarli
  • 1
  • 1
  • 1
  • 1
    You're missing an instance of regularpo. – erikvimz Dec 21 '14 at 19:05
  • 2
    By convention class names start with a capital letter. – Elliott Frisch Dec 21 '14 at 19:05
  • 1
    `System.out.println(regularpo.toString());` Apparently `regularpo` is a class name, not an object reference. `toString` is an instance method, meaning you must supply an *instance* of `regularpo` to use it. And you don't appear to create an instance of `regularpo` anywhere, so there's nothing else to use. – Hot Licks Dec 21 '14 at 19:07
  • possible duplicate of [What is the reason behind "non-static method cannot be referenced from a static context"?](http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – khelwood Dec 21 '14 at 19:09
  • And, as Eliot says, class names should start with an *UpperCase* letter, while variable names should start with *lowerCase*. It is much easier for others to understand your code if you stick with this convention. – Hot Licks Dec 21 '14 at 19:09

5 Answers5

1

You are trying to call a method of a class, when that method has been defined for (and only makes sense as) a method of an instance of that class. Maybe you mean to make an object of that class, and call its toString method, although I can't be sure from your code.

Scott Hunter
  • 44,196
  • 8
  • 51
  • 88
1

You can not access non-static methods by using classname.nonStaticMethodName. You need to instantiate your object using the new keyword. Basically, you create an instance of your object by regularpo r = new regularpo(2.0, 2.0). After that you can invoke r.toString();

Check out this SO-question for more info.

And this Oracle-tutorial explains class members well.

Community
  • 1
  • 1
wassgren
  • 16,439
  • 5
  • 53
  • 71
1

Suggestions:

1) Eliminate "regularpoTest". Just move "main()" into "regularpo".

2) Capitalize "RegularPo" (by convention, class names should start with a capital letter).

3) Make the RegularPo constructor actually save the initial values (not just set them to zero).

... and, most important ...

4) Your main should call RegularPo regularPo = new RegularPo (...). Then reference object instance "regularPo".

FoggyDay
  • 10,517
  • 4
  • 24
  • 33
  • hi! thanks for the help the only thing i don't understand is point 4... i should put in my main class (regularpoTest right?) what you've written but what do you mean with reference the instance regularPo? bear with me just started with all this ;) – Carlo Sarli Dec 22 '14 at 17:01
0

Try to make a object of class regularpo and call toString over that object

regularpo obj=new regularpo();
obj.toString();

Also as per conventions a class name must start with Upper case,so name your class asRegularpo

Abhishek
  • 880
  • 12
  • 28
0

toString() is a non static method in regularpro class , and we know that the non static belongs to an object so we need to create and object of same class and call it.

toString() is belongs to Object class so its non static method.

regularpo obj=new regularpo(); obj.toString();