0

Here's my code. My problem is that I'm trying to check if my user input variable, titleScanner, is equal to the title (which is specified earlier in the code) or if its equal to "exit". Even when I get my code to print out what titleScanner is equal to (exit when I type exit) the else if code for exit won't run.

    void getId() {
            Scanner titleScanner = new Scanner(System.in);
            System.out.println("Please enter the title of the game in order to obtain the ID Number");
            int loopbreak = 0;
            while (loopbreak == 0) {
                String titleVerification = titleScanner.nextLine();
                if (titleVerification == title) {
                    System.out.println(title + " has ID Number " + id);
                    loopbreak = 1;
                } else if (titleVerification == "exit") {
                    loopbreak = 1;
                } else {
                    System.out.println("Please enter a valid title or type exit to exit");
                }

            }

        }
  • Thanks so much :) I never looked up the question in that form – Eric Yan Jan 03 '16 at 05:21
  • Eric, also add some prints to see what is going on. Add these after the call to .nextLine(); and try w/various inputs (note the comment formatting screws up lines, so split @ the semicolons): System.out.println(" title=<" + title + ">" ); System.out.println("titleVerification=<" + titleVerification + ">" ); System.out.println("t==t : " +(title==title) ); System.out.println("tv==t : " +(titleVerification==title) ); System.out.println("tv.equals(t) : " +(titleVerification.equals(title)) ); System.out.println("tv.equalsIgnoreCase(t) : " +(titleVerification.equalsIgnoreCase(title)) ); – jgreve Jan 03 '16 at 05:49

0 Answers0