0

Hi I have this problem I get this error in the main function: "non static method cannot be referenced from a static context" all of the methods called are underlined with this error. I've tried a few things and I can't find a way to overcome it and it's doing my head in. Is there a way I can get rid of that error? thanks here is the code:

import java.util.Scanner;

public class Survey {

int age;
char gender;
char show;
int over30MY = 0, over30FY = 0, under30MY = 0, under30FY = 0;
int over30MN = 0, over30FN = 0, under30MN = 0, under30FN = 0;
int totalCalls;
int totalWatchers;
float perTotalWatchers;
float perU30M, perU30F, perO30M, perO30F, perTotalF, perTotalM;
float perTotalU30, perTotalO30, perTotal;

public static void main(String[] args) {       

    System.out.println("Thank you for your call,\nPlease take some time to answer a few questions");
    Info();
    collectData();
    getTotals();
    printTotals();

}//end of main

public void Info() { //Prints student Info

    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
}

public void collectData() {

    Scanner userInput = new Scanner(System.in); //Does everything

    //ask questions
    System.out.println("What is your age?\n");
    age = userInput.nextInt();

    System.out.println("Male or Female (Enter M or Y)");
    gender = userInput.next().charAt(0);
    gender = Character.toLowerCase(gender);

    System.out.println("Do you watch the show regularly? (Enter Y or N)");
    show = userInput.next().charAt(0);
    show = Character.toLowerCase(show);

    //store data
    if((age > 30) && (gender == 'm') && (show == 'y')) {       
        over30MY++;             
    }
    else if((age > 30) && (gender == 'f') && (show == 'y')) {
        over30FY++;
    }
    else if((age < 30) && (gender == 'm') && (show == 'y')) {
        under30MY++;
    }
    else if((age < 30) && (gender == 'f') && (show == 'y')) {
        under30FY++;
    }
    else if((age > 30) && (gender == 'm') && (show == 'n')) {
        over30MN++;
    }
    else if((age > 30) && (gender == 'f') && (show == 'n')) {
        over30FN++;
    }
    else if((age < 30) && (gender == 'm') && (show == 'n')) {
        under30MN++;
    }
    else if((age < 30) && (gender == 'f') && (show == 'n')) {
        under30FN++;

    }//end of if else

}//end of collectData

    public void getTotals() {//calculate totals for printing

        totalCalls = over30MY + over30FY + under30MY + under30FY + over30MN + over30FN +      under30MN + under30FN;

        totalWatchers = over30MY + over30FY + under30MY + under30MN;

        perTotalWatchers = Math.round(totalWatchers / totalCalls); //rounds percentage to nearest whole number

        perU30F = Math.round(under30FY / totalWatchers); //Total Under 30 Females
        perU30M = Math.round(under30MY / totalWatchers); //Total Under 30 Males
        perO30F = Math.round(over30FY / totalWatchers); //Total Over 30 Females
        perO30M = Math.round(over30MY / totalWatchers); //Total Over 30 Males

        perTotalF = Math.round(perU30F + perO30F); //Total Females
        perTotalM = Math.round(perU30M + perO30M); //Total Males

        perTotalU30 = Math.round(perU30F + perU30M); //Total Under 30
        perTotalO30 = Math.round(perO30F + perO30M); //Total Over 30
        perTotal = Math.round(perTotalF + perTotalM); //Total 

    }//end of getTotals

    public void printTotals () { //Prints the Totals

        System.out.println("Total People called is "+totalCalls);
        System.out.println("Number of people who watch the show regularly is "+totalWatchers);
        System.out.println("Percentage of those who watch the show is "+perTotalWatchers);      

        System.out.println("");
        System.out.println("");

    }//end of printTotals

}// end of class
user2704743
  • 1,223
  • 5
  • 15
  • 14

4 Answers4

3

You cant call non static method from static method. You have to create instance of it first.

public static void main(String[] args) {       

    System.out.println("Thank you for your call,\nPlease take some time to answer a few questions");
    Survey s = new Survey();
    s.Info();        
    s.collectData();
    s.getTotals();
    s.printTotals();

}//end 
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

you are calling non static methods

Info();
    collectData();
    getTotals();
    printTotals(); 

inside main method these are non static and main method is static method. Either make these methods as static or create an object of the class,then call these methods

import java.util.Scanner;

public class Survey {

int age;
char gender;
char show;
int over30MY = 0, over30FY = 0, under30MY = 0, under30FY = 0;
int over30MN = 0, over30FN = 0, under30MN = 0, under30FN = 0;
int totalCalls;
int totalWatchers;
float perTotalWatchers;
float perU30M, perU30F, perO30M, perO30F, perTotalF, perTotalM;
float perTotalU30, perTotalO30, perTotal;

public static void main(String[] args) {       

    System.out.println("Thank you for your call,\nPlease take some time to answer a few questions");
    Survey s=new Survey();
   s. Info();
    s.collectData();
    s.getTotals();
   s. printTotals();

}//end of main

public void Info() { //Prints student Info

    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
}

public void collectData() {

    Scanner userInput = new Scanner(System.in); //Does everything

    //ask questions
    System.out.println("What is your age?\n");
    age = userInput.nextInt();

    System.out.println("Male or Female (Enter M or Y)");
    gender = userInput.next().charAt(0);
    gender = Character.toLowerCase(gender);

    System.out.println("Do you watch the show regularly? (Enter Y or N)");
    show = userInput.next().charAt(0);
    show = Character.toLowerCase(show);

    //store data
    if((age > 30) && (gender == 'm') && (show == 'y')) {       
        over30MY++;             
    }
    else if((age > 30) && (gender == 'f') && (show == 'y')) {
        over30FY++;
    }
    else if((age < 30) && (gender == 'm') && (show == 'y')) {
        under30MY++;
    }
    else if((age < 30) && (gender == 'f') && (show == 'y')) {
        under30FY++;
    }
    else if((age > 30) && (gender == 'm') && (show == 'n')) {
        over30MN++;
    }
    else if((age > 30) && (gender == 'f') && (show == 'n')) {
        over30FN++;
    }
    else if((age < 30) && (gender == 'm') && (show == 'n')) {
        under30MN++;
    }
    else if((age < 30) && (gender == 'f') && (show == 'n')) {
        under30FN++;

    }//end of if else

}//end of collectData

    public void getTotals() {//calculate totals for printing

        totalCalls = over30MY + over30FY + under30MY + under30FY + over30MN + over30FN +      under30MN + under30FN;

        totalWatchers = over30MY + over30FY + under30MY + under30MN;

        perTotalWatchers = Math.round(totalWatchers / totalCalls); //rounds percentage to nearest whole number

        perU30F = Math.round(under30FY / totalWatchers); //Total Under 30 Females
        perU30M = Math.round(under30MY / totalWatchers); //Total Under 30 Males
        perO30F = Math.round(over30FY / totalWatchers); //Total Over 30 Females
        perO30M = Math.round(over30MY / totalWatchers); //Total Over 30 Males

        perTotalF = Math.round(perU30F + perO30F); //Total Females
        perTotalM = Math.round(perU30M + perO30M); //Total Males

        perTotalU30 = Math.round(perU30F + perU30M); //Total Under 30
        perTotalO30 = Math.round(perO30F + perO30M); //Total Over 30
        perTotal = Math.round(perTotalF + perTotalM); //Total 

    }//end of getTotals

    public void printTotals () { //Prints the Totals

        System.out.println("Total People called is "+totalCalls);
        System.out.println("Number of people who watch the show regularly is "+totalWatchers);
        System.out.println("Percentage of those who watch the show is "+perTotalWatchers);      

        System.out.println("");
        System.out.println("");

    }//end of printTotals

}// end of class

Note Making methods will cause problem in your case because you need make all variables like age,gender etc to static.So instead of making methods as static just create an object and call these methods

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

Do this:

Survey s = new Survey();
s.collectData();
s.getTotals();
s.printTotals();
metsburg
  • 2,021
  • 1
  • 20
  • 32
0
collectData();
getTotals();
printTotals();

These method are not static so you can't access without initiating.

You can have two possible solution. First make these method static

  public static void Info() {
  }

Second

Survey  s=new Survey ();
s.collectData();
s.getTotals();
s.printTotals();

Initialize and call these method as above.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115