These if else statements aren't giving me any output no matter what answer I give

Viewed 30
System.out

import java.util.Scanner;

System.out.println("Enter a Roman Numeral 1-8: ");
        Scanner keyboard = new Scanner(System.in);
        String roman_numeral = keyboard.next();
        String roman_numeral_upper = roman_numeral.toUpperCase();
        System.out.println(roman_numeral_upper);
        
        if(roman_numeral_upper == "I") {
            System.out.println("your number is 1");
    
        }
        else if (roman_numeral_upper == "II") {
            System.out.println("your number is 2");
        }
        else if (roman_numeral_upper == "III") {
            System.out.println("your number is 3");
        }
        else if (roman_numeral_upper == "IV") {
            System.out.println("your number is 4");
        }
        else if (roman_numeral_upper == "V") {
                System.out.println("Your number is 5");
        }
        else if (roman_numeral_upper == "VI") {
            System.out.println("Your number is 6"); 
        }
        else if (roman_numeral_upper == "VII") {
            System.out.println("your number is 7");
        }
        else if (roman_numeral_upper == "VIII") {
            System.out.println("Your number is 8");
        }
        
        }
        
        }

Is there perhaps something I'm missing? I've tried adding an else statement, and that always is what prints. is there some reason it is skipping through the if else statements and I just can't see it? It isn't giving me any error messages and im curious.

1 Answers

Strings in java are objects so instead of using

if (roman_numercal_upper == "I")

which performs a strict check that the two objects are the same instance, use

if (roman_numercal_upper.equals("I"))
Related