-2

I don't want to add all of the code as it is a game i am making currently. Anyways, I declared class_Selection, and Characterclass. The user enters a string for class_Selection. It displays the result and then proceeds into an if statement in which it uses that same input to decide on what to do afterwards. I am very new to java, and coding for that matter and this is my first project. The problem is probably simple. As you can see I placed it in all caps because I heard that if its in all caps no matter how the user enters it, it will accept that key, such as: MeLeE, That may not be true. The result produces as it goes through the rest of the program correctly even displays the results for class_Selection but when it gets to the if- statements it displays null.

Edit The problem is somehow character class is equal to null so when I System.out.println(Characterclass); I am receiving null.

I fixed the ==, thank you for that, but I need to fix CharacterClass somehow ending up to be null.

EDIT The problem was, in the IF-statement I had the answer in all caps, I know there is probably a way to do that, but I wasn't doing it right. If I enter the right key such as if it is MELEE, I enter MELEE it will display the results. Also I think when I did the system.out.println(Characterclass); , I didn't have a method that ran it so I am going to fix that. I got it to display the bonuses.

Thank you for the == problem, I am a step farther on my goal.

// Class Selection

    System.out.println("Which one did you excel in ( Class )? Melee, Magic, or Archery?"); // Class selection
    String class_Selection; // Hold the class name (make method with this to declare bonuses and things that have to do with the class he chose: such as If ( class_Selection == melee) 
    class_Selection = classSelection.next(); // User enters name of class they want
    System.out.println("You entered: " + class_Selection + (" Is this the class you want?, y or n?"));

    String yOrN;
    yOrN = defaultScanner.next();

    if (yOrN == "n") { // redo if selection is no
        System.out.println("Then which class would you like? Melee, Magic, Archery?");
        class_Selection = classSelection.next();
        System.out.println("You entered: " + class_Selection + (" Is this the class you want?, y or n?"));
        yOrN = defaultScanner.next();


    }else{ // Does nothing if y because the selection is correct
    }

    // Continue after class selection


        System.out.println("Your class is: " + class_Selection); // Final display
        System.out.println("\n");


        // This is where your selection takes effect and displays your bonuses

        if (class_Selection == "MELEE") {
            Characterclass = "melee";
            System.out.println("Melee = +5 Blade, +5 Blunt, +5 Hand-To-Hand, +5 Heavy Armor,");

        }
        if (class_Selection == "ARCHERY") {
            Characterclass = "Archery";
            System.out.println("+10 Archery, + 10 Light Armor");

        }
        if (class_Selection == "MAGIC") {
            Characterclass = "Magic";
            System.out.println(" +10 Arcane, +5 Light Armor");


}
        System.out.println(Characterclass);

}   

}

5 Answers5

2

if (yOrN == "n")!!! should be if (yOrN.equals("n")){...}

For any Object equality check, you should use Object#equals. For Object reference equality check you should use ==

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
2

Comparing Strings in Java, use String.equals(); not == operator. e.g. yOrN.equals("n")

The == operator checks to see if the two Strings reference the same String object. The equals() method compares if two Strings have the same characters within both Strings.

blackpanther
  • 10,998
  • 11
  • 48
  • 78
0

Compare Strings using .equals(), not using == which compares references. If you want a case insensitive comparison use .equalsIgnoreCase()

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

Comparison of Strings should be done using equals

if (yOrN == "n") {
// Code
}

Will become

if (yOrN.equlals("n")) {
// Code
}
Amarnath
  • 8,736
  • 10
  • 54
  • 81
0

Use:

if (yOrN.equals("n"))

Read this about how to compare Strings.

Community
  • 1
  • 1
joan
  • 2,407
  • 4
  • 29
  • 35