-2
import java.util.Scanner;

public class ShopSystem 
{
public static void main (String [] args)
{
    String plyrRace; int plyrRaceI;
    System.out.println("What is your race?");
    System.out.println("Orc");
    System.out.println("Elf");
    System.out.println("Human");

    Scanner scan = new Scanner(System.in);
    String userString = scan.next();

    if (userString.equalsIgnoreCase("orc"));

    {
        plyrRace = "orc";
        plyrRaceI = 0;
        System.out.println("You've chosen orc!");
    }


    else if (userString.equalsIgnoreCase("elf"));
    {
        plyrRace = "elf";
        plyrRaceI = 1;
        System.out.println("You've chosen elf!");
    }


    else if (userString.equalsIgnoreCase("human"));
    {
        plyrRace = "human";
        plyrRaceI = 2;
        System.out.println("You've chosen human!");
    }

    else
    {
    System.out.println("That is not a valid race; pick again.");

    }   
}

}

Helping my friend code a game, I was trying to work on an expansive shop system, which I needed races for. I wasn't able to use the else or else if statements without an error. I have no idea why I can't get the else statements to work. Any help would be amazing, as I'm new to Java.

Scorpion
  • 577
  • 4
  • 21
  • 16
    Remove the `semicolon` after each if/else if – Alexis C. Mar 14 '14 at 22:23
  • `if (...);` <-- problem here. Basically you tell to do nothing if the condition is satisfied! And the code in {} still executes (anonymous code blocks) – fge Mar 14 '14 at 22:25
  • I disagree that this is "a simple typographical error" (although I don't see a need to reopen it). "Typographical error" implies either your finger slipped on the keyboard or you have a brain fart. However, this problem has occurred so many times that I think there's something more fundamental going on, such as new programmers being taught that "you're supposed to put semicolons at the end of statements" without clarifying that it doesn't apply to `if`, `while`, etc. Not that I know what to do about the problem. – ajb Mar 15 '14 at 01:06

3 Answers3

2

As was said in a comment, you shouldn't have semicolons after the condition. So if(userString.equalsIgnoreCase("some race")) should only have a curly brace after it, not a semicolon. This is because the if clause is introducing a body of code - it's not a self-contained statement, so it should not end in a semicolon.

Epiglottal Axolotl
  • 1,048
  • 8
  • 17
2

As stated in the comments, remove all semi-colons after each if-paren

For example, change this

if (userString.equalsIgnoreCase("orc"));
{
    plyrRace = "orc";
    plyrRaceI = 0;
    System.out.println("You've chosen orc!");
}

to

if (userString.equalsIgnoreCase("orc"))  {
    plyrRace = "orc";
    plyrRaceI = 0;
    System.out.println("You've chosen orc!");
}

The reason for this is: When the semi-colon is left there, it is equivalent to this:

if (userString.equalsIgnoreCase("orc"))  {
}

{
    //This block is ALWAYS executed!

    plyrRace = "orc";
    plyrRaceI = 0;
    System.out.println("You've chosen orc!");
}

Which is definitely what you do not want.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
1

Remove the semicolon at the end of the if statements.

Also, this is a perfect example when a switch statement should be used.

selator
  • 76
  • 2
  • 5
  • 1
    Unfortunately, there's no `switchIgnoreCase` statement. Maybe we should have pushed for one in Java 8 ... But `switch` can be used like `switch(userString.lowerCase()) { ...`, as long as we aren't using Georgian or Turkish alphabets. – ajb Mar 14 '14 at 22:47
  • There's an [overload](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toLowerCase%28java.util.Locale%29) of toLowerCase which takes a locale as an argument, so you can specify it and don't have to rely on the user's current one. – selator Mar 14 '14 at 23:11