michael mathews
michael mathews

Reputation: 9

I need to call an array from another method, then use it

I am trying to use the inventory as a separate method, and originally I had it in a separate class altogether, but it didn't seem to work from another class so I just decided another method would be good enough because I can still call it separate from the store. But for some reason its not working correctly and just terminates?

import java.util.Scanner;

public class Shop {
    public static void main(String Args[]) {
    }

    public static void Store(String Inventory[]) {

        Scanner choose = new Scanner(System.in);
        Scanner choice = new Scanner(System.in);
        int gold = 100;

        String[] Weapon = new String[3];
        Weapon[0] = "Sword";
        Weapon[1] = "Dagger";
        Weapon[2] = "Staff";

        System.out.println("Hello today we have\n 1.Rusty Sword $30 \n 2. Old Dagger $70 \n 3. Worn Staff $80:");
        System.out.println("Hit 1 to find your item.");
        int pick = choose.nextInt();

        do {
            System.out.println("You have " + gold + " moneys.");

            int x;
            x = choice.nextInt();
            if (x == 1 && gold >= 30) {
                Inventory[0] = Weapon[0];
                gold = gold - 30;
                System.out.println("Gold: " + gold);
                System.out.println("Inventory:\n " + Inventory[0]);
            } else if (x == 2 && gold >= 70) {
                Inventory[1] = Weapon[1];
                gold = gold - 70;
                System.out.println("Gold: " + gold);
                System.out.println("Inventory:\n 1." + Inventory[0] + "\n2." + Inventory[1]);
            } else if (x == 3 && gold >= 80) {
                Inventory[2] = Weapon[2];
                gold = gold - 80;
                System.out.println("Gold: " + gold);
                System.out.println("Inventory:\n 1." + Inventory[0] + "\n2." + Inventory[1] + "\n3." + Inventory[2]);
            } else {
                System.out.println("Sorry, you are one poor soul.");
                break;
            }
        } while (pick == 1);
        choose.close();
        choice.close();
    }

    public static void inv() {
        String InventoryB[] = new String[10];
        InventoryB[0] = "";
        InventoryB[1] = "";
        InventoryB[2] = "";
        InventoryB[3] = "";
        InventoryB[4] = "";
        Store(InventoryB);

    }
}

Upvotes: 0

Views: 1425

Answers (1)

Lokesh
Lokesh

Reputation: 7940

Your main method is empty!! So effectively a blank program.

public static void main(String Args[]){

    }

Put something in main method for it to work.

Looking at your program you main method should be:

public static void main(String Args[]){
    Shop.inv();
        }

Upvotes: 2

Related Questions