2

This code does not run for some reason. This is a simple RPG game. `
import java.util.Scanner;

    public class My_first_RPG {
        double exp;
        double attack;
        double vitality;
        double armor;
        double mana;

        int skill() {
            return (int) (attack*mana);
        }
    }
    class character {
        public static void main(String args[]) 
            throws java.io.IOException {
            My_first_RPG Mage = new My_first_RPG();
            My_first_RPG Warrior = new My_first_RPG();
            My_first_RPG Archer = new My_first_RPG();
            My_first_RPG Dwarven_Mech = new My_first_RPG();
            My_first_RPG Steel_Golem = new My_first_RPG();
            int world[][] = new int[10][10];
            world[1][1]=2; 

            Mage.attack = 75;
            Mage.vitality = 1;
            Mage.armor = 10;
            Mage.mana = 200;

            Warrior.attack = 100;
            Warrior.vitality = 2;
            Warrior.armor = 20;
            Warrior.mana = 100;

            Archer.attack = 65;
            Archer.vitality = 1;
            Archer.armor = 15;
            Archer.mana = 150;

            Dwarven_Mech.attack = 125;
            Dwarven_Mech.vitality = 0.5;
            Dwarven_Mech.armor = 5;
            Dwarven_Mech.mana = 75;

            Steel_Golem.attack = 50;
            Steel_Golem.vitality = 0;
            Steel_Golem.armor = 30;
            Steel_Golem.mana = 50;

            System.out.println("Choose your Hero.");
            System.out.println("1. Mage");
            System.out.println("2. Warrior");
            System.out.println("3. Archer");
            System.out.println("4. Dwarven Mech");
            System.out.println("5. Steel Golem");

            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();

            switch(choice) {
                case 1:
                    System.out.println("You are now a Mage.");
                case 2:
                    System.out.println("You are now a Warrior.");
                case 3:
                    System.out.println("You are now a Archer.");
                case 4:
                    System.out.println("You are now a Dwarven Mech.");
                case 5:
                    System.out.println("You are now a Steel Golem.");
            }   
        }
        class Grasslands {

        }

    }

` Even though i have the public static void main, the code cannot run. Is there a problem? Also, the array and the class Grasslands arent finished yet.

0lukasz0
  • 3,155
  • 1
  • 24
  • 40
ensj
  • 33
  • 4
  • 1
    What do you mean by 'the code cannot run'. Does it try to run but hit an error, or does it end with no IO, or is there a problem in your IDE? (note that your main method catches IOExceptions; if there's an IOException, the main method will exit without an error message. This is most probably your problem. Why are you catching that exception over the entire program life? Why not validate user input?) – Anti Earth Jul 27 '13 at 07:17
  • What is the command you use to start your application? – JB Nizet Jul 27 '13 at 07:19

1 Answers1

1

The main method must be inside a public class so that it can be accessed from "outside". See here: What if main method is inside "non public class" of java file?

Note that there can only be one public class per file.

Community
  • 1
  • 1
Kon
  • 10,702
  • 6
  • 41
  • 58
  • 3
    Nope, the class doesn't have to be public. – JB Nizet Jul 27 '13 at 07:19
  • @JBNizet Really? The provided SO question directly states otherwise – Anti Earth Jul 27 '13 at 07:21
  • Test it. Shouldn't be hard. I just did it, with Java 7, and it runs without any problem. – JB Nizet Jul 27 '13 at 07:21
  • Are you suggesting that SO question was answered incorrectly, or there's a subtle difference in circumstance you're unhelpfully not identifying? :\ – Anti Earth Jul 27 '13 at 07:22
  • I don't have the full Java history in mind. Maybe it was impossible in the past. But with Java 7 at least, a main method in a non public class, even if defined in the same file as a public class, can be executed without problem. – JB Nizet Jul 27 '13 at 07:26
  • I find that answer to be quite confusing, but @JBNizet is correct in that the class containing `main` does not have to be public. It is just simpler and more straightforward if it is. And if the main class **is** `public`, no other class in the same source file can be. Also note that putting more than one class per source file is generally frowned upon (though I often do it for the sake of making an SSCCE). – Andrew Thompson Jul 27 '13 at 07:41
  • I had the same impression but @JBNizet is correct (+1). I can succesfully run in java 6 from command line with `java character` – c.s. Jul 27 '13 at 08:14
  • +1 This is indeed correct. The behaviour is confusing due to the unusual class structure in one file. A case of http://stackoverflow.com/questions/2336692/java-multiple-class-declarations-in-one-file. Moving `main()` out of `character` to `My_first_RPG` and making that one public instead of `character` fixes the program. Better would be not having more than one class in the same file in the first place. – kiheru Jul 27 '13 at 08:36
  • The answer could be more clearly phrased though. I think it is highly confusing to new java users. – kiheru Jul 27 '13 at 08:49
  • Also note when there are other dissenting answers. Just because one answer is checked as accepted by the original poster, this does not prove it is more correct than any other. Seek answers on other questions as well. For example, see also http://stackoverflow.com/questions/8949801/is-there-any-benefit-of-writing-public-classes-for-the-main-method – WarrenT Jul 27 '13 at 13:37