Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6786951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:18:24+00:00 2026-05-26T17:18:24+00:00

import java.util.*; import java.io.*; public class LootGenerator{ public static void main (String[] args) throws

  • 0
 import java.util.*;
 import java.io.*;

public class LootGenerator{

public static void main (String[] args)
    throws FileNotFoundException{
        System.out.println("This program simulates the random item generator");
        System.out.println("from the game Diablo II.  Happy hunting!");
        System.out.println();
        String file="monstats.txt";
        Monster[] array = getArray(file);
        //Monster alex=getRandomMonster(array);
        }

public Monster getRandomMonster(Monster[] array){
  int randMonster = (int)(Math.random() * array.length);
    return array[randMonster];
}

public Monster[] getArray (String file)
    throws FileNotFoundException{
Scanner sc = new Scanner (new File (file));
Monster[] array = new Monster[sc.nextInt()];
sc.next(); //takes away the word Class
sc.next(); //takes away the word Type
sc.next(); //takes away the word Level
sc.next(); //takes away the word TreasureClass
for(int a = 0; a < array.length; a++)
{
    array[a] = new Monster(sc.next(), sc.next(), sc.nextInt(), sc.next());
}
return array;
}

}

  class Monster{
    private String monsterClass;
private String type;
    private int level;
    private String treasureClass;

public Monster(String myClass, String myType, int myLevel, String myTreasureClass){
    monsterClass = myClass;
      type = myType;
    level = myLevel;
    treasureClass = myTreasureClass;
}

public String getMonsterClass(){
    return monsterClass;
}

public String getType(){
    return type;
}

public int getLevel(){
    return level;
}

public String getTreasureClass(){
    return treasureClass;
}

}

I can’t figure out what is wrong with my program…any advice? I keep getting that it can’t be referenced from a static context – the line Monster[] array = getArray(file) that is. The object of the program is to randomly generate a monster from a text file – the assignment is here if you need to look at the text file itself: http://www.cis.upenn.edu/~cis110/hw/hw06/index.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:18:24+00:00Added an answer on May 26, 2026 at 5:18 pm

    You’re trying to call a regular method from a static method, which isn’t possible. You could instantiate LootGenerator and then call the method or make the method static (simply add the static modifier to the method).

    One solution:

    public static void main (String[] args)
        throws FileNotFoundException {
            System.out.println("This program simulates the random item generator");
            System.out.println("from the game Diablo II.  Happy hunting!");
            System.out.println();
            String file = "monstats.txt";
            LootGenerator lg = new LootGenerator();
            Monster[] array = lg.getArray(file);
            Monster alex = lg.getRandomMonster(array);
    }
    

    You can find more information about instance and class members here.

    In short, instance methods can only be called on instances of a class. Static methods belong to the class and can be called from anywhere, but they can’t access instance variables or call instance methods unless they actually have a reference to an instance of a class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.*; public class oddNumbers { public static void main(String[] args) { System.out.println(enter two
import java.util.Scanner; public class Mal { public static void main(String[] args) { System.out.println(Welcome); Scanner
import java.util.Scanner; public class Main extends Hashmap{ public static void main(String[] args) { Hashmap
import java.util.Scanner; public class EP55Out { public static void main(String[] args) { Scanner in
import java.util.scanner; import javax.swing.JOptionPane; public class FirstHomeJavaApplet{ public static void main(String[] args){ int num1=2;
import java.util.*; public class Test { public static void main(String[] args) { Map<String,String> map
import java.util.ArrayList; public class WTFAMIDOINGWRONG { public static void main(String[] args) { ArrayList<Integer> intsAR
import java.util.Scanner; import java.lang.String; public class Test { public static void main(String[] args) {
import java.util.*; public class Guess { public static void main (String[] args) { final
import java.util.*; public class MyClass { public static void main(String[] args) { List<String> a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.