0

I am confused as to why I have a non static method error. It says "non-static method getGrade(double) cannot be referenced from a static context getGrade(myGPA);" I have been reading on what non static method. I get it I can't call something that doesn't exist. Since you haven't created an object, the non-static method doesn't exist yet. A static method (by definition) always exists. So this is my code below, and the error says "non-static method getGrade(double) cannot be referenced from a static context getGrade(myGPA);" I created an object of that, but gave me a "cannot find symbol error." I know it is something minor, can someone please guide me as to what I can do? THANK YOU in advance!

The program is supposed to take the user's input (GPA, in decimal format, and covert that into percentile, and response to the user.

 import java.util.Scanner;

  public class GradeCalc {
 public double myGPA;
 public double numGrade;

  public static void main (String[] args) {
    Scanner myScanner = new Scanner (System.in);
    System.out.println("Enter your GPA.");
    double myGPA= myScanner.nextDouble();

    getGrade(myGPA);
    System.out.println(myGPA);
    }

    public double getGrade(double myGPA) {
    if(myGPA==0.0 || myGPA==0.8 || myGPA==0.9 || myGPA==1.0 || myGPA==1.1 ||           
      ||myGPA==1.8 || myGPA==1.9 || myGPA==2.0 || myGPA==2.1 || myGPA==2.2 || myGPA==2.3        || myGPA==2.4 || myGPA==2.5 || myGPA==2.6 || myGPA==2.7 || myGPA==2.8 || myGPA==2.9 || myGPA==3.0 || myGPA==3.1 || myGPA==3.2 || myGPA==3.3 || myGPA==3.4 || myGPA==3.5 || myGPA==3.6 || myGPA==3.7 || myGPA==3.8 || myGPA==3.9 || myGPA==4.0 ) {
        double numGrade = (myGPA*10+55);
    }
        if (myGPA == numGrade)
            System.out.println(numGrade);
    else  {
        System.out.println("Try again.");
    }
     return numGrade;
}
}
DT7
  • 1,615
  • 14
  • 26
Rid S
  • 9
  • 1
  • No offense, but that `if` is _so_ ugly. Not to mention it breaks when I enter 1.15. Why didn't you just write `if(myGPA <= 4.0)`? – CompuChip Nov 18 '13 at 14:50

4 Answers4

2

Non-static method cannot be call from a static context.

You can create an instance of GradeCalc class, and use this instance to call the non-static method.

Like,

GradeCalc gradeCalc = new GradeCalc();
gradeCalc.getGrade(myGPA);
Mengjun
  • 3,159
  • 1
  • 15
  • 21
1

You are trying to call a non-static method from a static one (main), which is non possible because your GradeCalc object is not defined ; either you can declared getGrade as a static method (but you can't use attributes), or create a GradeCalc object inside your main and call the method with it.

Best would be to separate main method and GradeCalc class, by creating a class reserved to main ;)

Jérémy Dutheil
  • 6,099
  • 7
  • 37
  • 52
0

Change

public double getGrade(double myGPA)

To

public static double getGrade(double myGPA)
William Riley
  • 878
  • 8
  • 13
0

Try

import java.util.Scanner;

public class GradeCalc {
    public double myGPA;
    public double numGrade;
    Scanner myScanner;

    private void readGPA() {
        myScanner = new Scanner(System.in);
        System.out.println("Enter your GPA.");
        myGPA = myScanner.nextDouble();
        myScanner.close();
    }

    public void computeGrade() {
        if (myGPA == 0.0 || myGPA == 0.8 || myGPA == 0.9 || myGPA == 1.0
                || myGPA == 1.1 || myGPA == 1.8 || myGPA == 1.9 || myGPA == 2.0
                || myGPA == 2.1 || myGPA == 2.2 || myGPA == 2.3 || myGPA == 2.4
                || myGPA == 2.5 || myGPA == 2.6 || myGPA == 2.7 || myGPA == 2.8
                || myGPA == 2.9 || myGPA == 3.0 || myGPA == 3.1 || myGPA == 3.2
                || myGPA == 3.3 || myGPA == 3.4 || myGPA == 3.5 || myGPA == 3.6
                || myGPA == 3.7 || myGPA == 3.8 || myGPA == 3.9 || myGPA == 4.0) {
            numGrade = (myGPA * 10 + 55);
        }
    }

    public void showResult() {
        if (myGPA == numGrade) {
            System.out.println(numGrade);
        } else {
            System.out.println("Try again.");
        }
    }

    public static void main(String[] args) {
        GradeCalc gradeCalc = new GradeCalc();
        gradeCalc.readGPA();
        gradeCalc.computeGrade();
        gradeCalc.showResult();
    }

}
Sandhu Santhakumar
  • 1,678
  • 1
  • 13
  • 24
  • Hint: the solution **is not** marking the other methods and fields as `static`. That's a **bad design** and OP won't learn the real meaning of `static` field/methods. – Luiggi Mendoza Nov 18 '13 at 14:54