1

I'm making a Character Creator for fun and I seem have run into yet another java problem! I tried googling around for a bit but I didn't seem to find a working solution... I'm trying to use a switch statement for the compiler to output certain information depending on what class (Knight, Archer, Mage) the user chose, but when inputting my code I get error messages

My code so far (cleaned up a bit) is :

    String name;
    String className;
    int attPoints;

    System.out.println("Welcome to 'GameName's' Character Creator 2.0!\n");
    Thread.sleep(500);
    System.out.print("First off, what do you want your characters name to be? \n\nName : ");
    name = Scan.nextLine();
    Thread.sleep(500);
    System.out.print("\nYou are now to be known as "+ name + "!");

    System.out.print("\n\n" + name + ", what class do you want to be? ");
    System.out.print("\n\nClasses available :\nKnight");
    Thread.sleep(1500);
    System.out.print("\nMage");
    Thread.sleep(300);
    System.out.print("\nDruid");
    Thread.sleep(300);
    System.out.print("\nNinja");
    Thread.sleep(300);
    System.out.print("\nArcher");
    Thread.sleep(300);
    System.out.print("\nAdventurer");
    Thread.sleep(300);
    System.out.print("\nBerserker");
    Thread.sleep(300);

    System.out.print("\n\nClass : ");
    className = Scan.next();

    Class userClass = Class.valueOf(className);
    Thread.sleep(500);
    System.out.println("\nCongratulations! Your class is now : "+ className + "!");
    Thread.sleep(500);


      // This is where I get an error.
      // - Syntax error, insert "enum Identifier" to complete EnumHeaderName    
      // - Syntax error, insert "EnumBody" to complete BlockStatement
  // - Syntax error on token "void", @ expected

    public void setClass()
        switch (Class) {

        case Knight:
            System.out.println(" Various lore about knights ");

            break;

    }
}

I THINK I may be trying to create a class inside another class - but when I tried putting it outside it got another error... Also, I have an int called attPoints, and after I choose a class I want to add 10 to it, but unsure how.

5
  • First, never use names in java.lang for your own types; use CharacterClass or something similar instead. Presumably you're using an enum Class, and it looks like the problems are there. (1) Post the code for the enum. (2) Compiler errors specify where the errors are occurring. Post that information. Commented Jan 15, 2014 at 17:15
  • 3
    switch (userClass), not switch (Class) Commented Jan 15, 2014 at 17:16
  • By the way: In Java, enum types are full classes. Instead of using switch, you generally should define methods on the enum and provide the implementations for each value. Commented Jan 15, 2014 at 17:19
  • Okay, so my enum is : public enum Class {Knight, Mage, Druid, Ninja, Archer, Adventurer, Berserker} And the errors I posted in the first code box all appear where I said "// This is where I get an error." Commented Jan 15, 2014 at 17:29
  • 1
    And it may be a good practice to use proper meaningful name when naming Classes or Enums - in this case GameCharacter may be a good name to avoid confusion. Commented Jan 15, 2014 at 17:29

1 Answer 1

1

You're switching on Class which is a Java type. Take a look at this tutorial: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

You probably want to do something like:

switch (userClass) {
    case Class.KNIGHT:
        System.out.println(" Various lore about knights ");
        break;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Class isn't reserved, class is.
Also in order to avoid writing EnumName.x for each case, the OP can use import static to get all values of the enum so that switch(x) can be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.