0

So I am writing a user menu using a simple while loop and case breaking but having an issue. I need to use chars instead of ints for my cases (really using a string Scanner for simplicity) and for the most part it works. But when I enter D (my exit case) the loop does not break. Not sure why, I have done this with ints many times without an issue...

import java.util.Scanner;

public class Postfix_Notation {
 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  String choice = "Z";

  while (choice != "D") {
   System.out.println("Enter A, B  or C to perform an operation or enter D to exit.");
   System.out.println("A- Evaluate a user input postfix expressions");
   System.out.println("B- Convert, display infix expressions to postfix expressions then evaluate and display the result of thepostfix expression");
   System.out.println("C- Reads words from a the text file (hangman.txt) in a LinkedList and use an iterator on it to displays all the words (duplicates allowed) in descending alphabetical order");
   System.out.println("D- Exit");
   choice = new Scanner(System.in).next();

   switch (choice) {
    case "A":
     System.out.println("Enter a string: ");
     String s = new Scanner(System.in).next();
     System.out.println("All possible permutations of " + s + " are: ");
     System.out.println("\n");
     break;

    case "B":
     Scanner input2 = new Scanner(System.in);

     System.out.println("Enter a hex string: ");
     String hexString = input2.next();

     System.out.println("The decimal equivalent of " + hexString + " is " + (hexString));
     System.out.println("\n");
     break;

    case "C":
     Scanner input = new Scanner(System.in);

     System.out.println("Enter a binary string: ");
     String binaryString = input.next();

     System.out.println("The decimal equivalent of " + binaryString + " is " + (binaryString));
     System.out.println("\n");
     break;

    case "D":

     System.out.println("Program Ended");

     break;

    default:
     System.out.println("Invalid Input");
   }
  }
 }
}
Jens Bannmann
  • 4,845
  • 5
  • 49
  • 76

1 Answers1

1

So while(!choice.equals("D")) do the job. Check this link: How do I compare strings in Java?. You compared references instead of values.

Michu93
  • 5,058
  • 7
  • 47
  • 80
  • I tested this in my compiler to make sure. It was in fact the only problem with their code. – Jeffrey Phillips Freeman Apr 21 '18 at 21:28
  • Tested this and it did the trick, thanks very much! So I read the link you gave me and I have a follow up question. So "Choice" is a reference or object and "D" is the value correct? So by comparing the reference to the value it does nothing, but shouldn't the compiler throw an error? I know its not the same as comparing different data types but it still isn't logically correct. – Ih8th3c0ld Apr 21 '18 at 21:33
  • Reference is pointer to the object in memory. Both "Choice" and "D" have references but you don't want to check if it's in the same place in memory, you are interested if value of "Choice" and value of "D" are the same, you don't really care where they are stored. – Michu93 Apr 22 '18 at 09:17