0

I don't know why the lines won't print in the while loop. I can't exit either. Please help. The program runs but nothing inside the while loop will print.

public class RomanNumeralHelper {

    /**
     * (Insert a brief description that describes the purpose of this method)
     *
     * @param args
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String myRoman;
        String upperRoman;

        System.out.print("Enter a roman numeral [Q | q to quit]: ");
        myRoman = in.next();
        upperRoman = myRoman.toUpperCase();

        while (upperRoman != "Q") {
            if (upperRoman == "I") {
                System.out.println(">> 1");
            } else if (upperRoman == "II") {
                System.out.println(">> 2");
            } else if (upperRoman == "III") {
                System.out.println(">> 3");
            } else if (upperRoman == "IV") {
                System.out.println(">> 4");
            } else if (upperRoman == "V") {
                System.out.println(">> 5");
            }

            System.out.print("Enter a roman numeral [Q | q to quit]: ");
            myRoman = in.next();
            upperRoman = myRoman.toUpperCase();
        }

        System.out.println("Good Bye!");

        // (5) Closes the in object to avoid a resource leak.
        in.close();
    }
}
Tiny
  • 27,221
  • 105
  • 339
  • 599

2 Answers2

3

Compare strings with equals() not with ==

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
0

You could also clean it up by not changing it to uppercase and using .equalsIgnoreCase() to compare strings.

Also, you could include the first print inside the while loop rather than repeating at the end of the loop. These are just little tips to clean up your code a little bit.

GermaineJason
  • 589
  • 6
  • 15