James Clarke
James Clarke

Reputation: 29

Java: method from another class can't be resolved

I am new to java. I have created an object of a class but when I try and access a method from the object, it says it cant be resolved. The 2 classes are below

import java.util.Scanner;

public class setup {
    static Scanner input = new Scanner(System.in);

    String goverment;
    int happyness;
    double money;
    int population = 1000000;


    public setup() {
    }

    public void statsSetup() {
        System.out.println("Choose a goverment: 1. democracy 2. monarchy 3. dictatorship");
        goverment = input.nextLine();

        if (goverment.equals("1"))
        {
            happyness = 75;
            money = 250000.0;

        }
        else if (goverment.equals("2"))
        {
            happyness = 50;
            money = 500000.0;
        }
        else if (goverment.equals("3"))
        {
            happyness = 25;
            money = 750000.0;
        }
        else
        {
            System.out.println("ENTER A VALID VALUE");
        }
    }

    public int getHappyness() {
        return happyness;
    }

    public double getMoney() {
        return money;
    }

    public int getPopulation() {
        return population;
    }
}

import java.util.Scanner;

public class gameLoop  {
    static Scanner input = new Scanner(System.in);

    static int turn = 0;

    public gameLoop() {
    }

    public static void main(String[] args) {
        setup setupGov = new setup();
    }

    public void loop() {
        while (true) {
            System.out.println("Turn: "+turn);
            ***System.out.println("happyness: " + setupGov.getHappyness() + " money: £" + setupGov.getMoney() + " population: " + setupGov.getPopulation());***
            input.nextLine();
            turn++;
        }
    }
}

the error occurs in the gameLoop class when I try and print the variables from the setup class

Upvotes: 1

Views: 1159

Answers (1)

Amr Alaa
Amr Alaa

Reputation: 1176

move setup setupGov = new setup(); outside the main function, put it right under static int turn = 0;

And I strongly suggest that you find a good programming basics course, there is many out there

Upvotes: 2

Related Questions