JeyesElite
JeyesElite

Reputation: 73

Having problems using a Switch statement in Java

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.

Upvotes: 1

Views: 312

Answers (1)

Mike B
Mike B

Reputation: 5451

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;
}

Upvotes: 1

Related Questions