0

I'm new to learning Java. I started about a week ago, and have spent around 10 hours a day learning, and I'm not sure if I'm behind where I "should be" (another concern I'm dealing with) I'm aware that my code is probably not the most efficient, but I have to start somewhere, and I've typed most of this out of memory from the cosmos of information I've read in the last week. Right now, I'm just trying to make games or apps to help solidify concepts of programming. To put what I've learned to use and make this information relevant.

my question is, essential, why are my "if" statements going directly to the "else"?

public class ClassSelect {

public static String className;

public static void pickPlayer() {

    Scanner scan = new Scanner(System.in);
    System.out.println("Pick your class." + "\n" + "[F]ighter" + "\n"
            + "[W]izard");
    String scanClass = scan.nextLine();

    if ((scanClass == "f") || (scanClass == "F")) {
        System.out.println("You picked the Fighter");
        String classNameF = "Fighter";
        className = classNameF;
        try {
            File file = new File("saveData.txt");
            BufferedWriter output = new BufferedWriter(new FileWriter(file));
            output.write(className);
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    else if ((scanClass == "w") || (scanClass == "W")) {
        System.out.println("You picked the Wizard");
        String classNameW = "Wizard";
        className = classNameW;
        try {
            File file = new File("saveData.txt");
            BufferedWriter output = new BufferedWriter(new FileWriter(file));
            output.write(className);
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("You didn't choose either.");
    }
}
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
Surlistyc
  • 3
  • 2
  • On this site the RPG tag refers to a programming language. Please read our tag info before using them. Thank you. – WarrenT Aug 05 '13 at 13:54

4 Answers4

3

For string comparison, you must use equals method of Object class. Your if statements will become like - if(scanClass.equals("f"))...

Remember, == compares reference and not the actual object.

Ved
  • 8,577
  • 7
  • 36
  • 69
0

Always compare Strings using equals.

  • == compares the references.

  • equals() on the other hand compares the values.

    if ((scanClass.equals("f") || (scanClass.equals("F"))) 
    
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
0

You should be using scanClass.equalsIgnoreCase("f") in if statements because == operator compares references not values.

Nils
  • 806
  • 1
  • 9
  • 24
0

The operator, ==, tests to see if two object reference variables refer to the exact same instance of an object.

The method, .equals(), tests to see if the two objects being compared to each other are equivalent -- but they need not be the exact same instance of the same object. So

 if ((scanClass == "f") || (scanClass == "F")) 

evaluates to false and control goes to the else block .

Shuhail Kadavath
  • 448
  • 3
  • 13