-3

New to java. Do not understand error. Basically trying to return value to then determine output but error "Cannot make static reference to non static field appears on line 13" in the class bosscalc. Return values from operators class.Please help. I have indicated line 13 in the class bosscalc. Thanks

package calculator;

import java.util.Scanner;

public class bosscalc {

    Scanner input = new Scanner(System.in);

    public static void main(String args[]) {
        operators operatorobjects=new operators();
        String answer;

        System.out.println("What would you like to do? ");
        answer =input.nextLine();  -------------------------LINE 13

         if (answer=="a"){

             double adding = operatorobjects.add();          
             }

         if (answer=="s") {
             double subtrat = operatorobjects.sub();
         }


         if (answer=="m") {
             double multiply = operatorobjects.sub();        
         }
    }

}

Class operators:

package calculator;

import java.util.Scanner;

public class operators {

    double add() {
        double n1,n2,a;
        Scanner input=new Scanner(System.in);
        System.out.print("Enter number 1 ");
        n1=input.nextDouble();      

        System.out.print("Enter number 2 ");
        n2=input.nextDouble();;

        a=n1+ n2;
        return a;
    }

    double sub() {
        double n1,n2,d;
        Scanner input=new Scanner(System.in);
        System.out.print("Enter number 1 ");
        n1=input.nextDouble();      

        System.out.print("Enter number 2 ");
        n2=input.nextDouble();;

        d=n1 - n2;
        return d;
    }

    double m() {
        double n1,n2,m;
        Scanner input=new Scanner(System.in);
        System.out.print("Enter number 1 ");
        n1=input.nextDouble();      

        System.out.print("Enter number 2 ");
        n2=input.nextDouble();;

        m=n1/n2;
        return m;
    }

}
J-Alex
  • 6,881
  • 10
  • 46
  • 64
Bobs
  • 1
  • 2
    Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, _a specific problem or error_ and _the shortest code necessary_ to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Joe C Jun 25 '17 at 09:46
  • Use `static Scanner input` when declaring it, or better yet just declare your scanner as a method level variable inside `main()`. And by the way, I'll bet you have other problems. – Tim Biegeleisen Jun 25 '17 at 09:53

2 Answers2

0

As the error message says: From a static context (your static main function) you cannot reference a non-static variable (input). You can fix it by making input static, i. e. declare it as follows:

static Scanner input = new Scanner(System.in);
Patrick
  • 184
  • 1
  • 1
  • 12
0

I have spent five minutes changing (refactoring) your code. There were a few simple errors. I have moved everything into a single class, and added some comments.

There are lots of improvements which can be made. But this is all down to practice and experience:

import java.util.Scanner;

public class Operators {
    /**
    * add numbers
    * @return n1 + n2
    */
    double add() {
        double n1, n2, a;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number 1 ");
        n1 = input.nextDouble();

        System.out.print("Enter number 2 ");
        n2 = input.nextDouble();
        a = n1 + n2;
        return a;
    }

    /**
     * subtract numbers
     * @return n1 - n2
     */
    double sub() {
        double n1, n2, d;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number 1 ");
        n1 = input.nextDouble();

        System.out.print("Enter number 2 ");
        n2 = input.nextDouble();

        d = n1 - n2;
        return d;
    }

    /**
     * multiply numbers
     * @return n1 * n2
     */
    double multiply() {
        double n1, n2, m;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number 1 ");
        n1 = input.nextDouble();

        System.out.print("Enter number 2 ");
        n2 = input.nextDouble();

        m = n1 * n2;
        return m;
    }

    /**
     * divide numbers
     * @return n1 / n2
     */
    double divide() {
        double n1, n2, m;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number 1 ");
        n1 = input.nextDouble();

        System.out.print("Enter number 2 ");
        n2 = input.nextDouble();

        m = n1 / n2;
        return m;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Operators operatorobjects = new Operators();
        String answer;

        System.out.println("What would you like to do? ");
        answer = input.nextLine();

        /**
         * String equality use String.equals()
         */
        if (answer.equals("a")) {

            double adding = operatorobjects.add();
            /**
             * Debug output println
             */
            System.out.println("adding = " + adding);

        } else if (answer.equals("s")) {

            double subtract = operatorobjects.sub();
            System.out.println("subtract = " + subtract);

        } else if (answer.equals("m")) {

            double multiply = operatorobjects.multiply();
            System.out.println("multiply = " + multiply);

        } else if (answer.equals("d")) {

            double divide = operatorobjects.divide();
            System.out.println("divide = " + divide);

        }
        /**
         * More debug exiting
         */
        System.out.println("exiting");
    }
}

I have added a divide method, and renamed to multiply. The output from running is:

What would you like to do? 
a
Enter number 1 10
Enter number 2 10
adding = 20.0
exiting


What would you like to do? 
s
Enter number 1 10
Enter number 2 2
subtract = 8.0
exiting

What would you like to do? 
m
Enter number 1 2
Enter number 2 5
multiply = 10.0
exiting

What would you like to do?  
d
Enter number 1 6
Enter number 2 3
divide = 2.0
exiting
chocksaway
  • 870
  • 1
  • 10
  • 21