0

when I input any number into my scanner, it returns the else statement ALWAYS, and i'm stuck in an infinite loop. Can anyone help me out? I need to make the storyChoice variable String, and not Int because I plan on adding more else if statements that require the user input to be a string.

    String storyChoice = keybd.next(); 

    //This loop checks the scanner to see if the user has inputted an 
    //appropriate answer, as soon as the user inputs an appropriate answer
    //The while loop terminates. 
    while(storyChoice != "1"|| storyChoice != "2" || storyChoice != "3") 
    {
        if(storyChoice == "1")
        {
            Sword story = new Sword();
            story.intro();
            break; 
        }
        else if (storyChoice == "2")
        {
            System.out.println("Not coded yet, so stay tuned!"); 
            break;
        }
        else if(storyChoice == "3")
        {
            System.out.println("Not coded yet, so stay tuned!"); 
            break;
        }
        else
        {
            System.out.println("Please choose an appropriate answer"); 
            storyChoice = keybd.next(); 
        }
    }

Thank you.

Sean G
  • 351
  • 2
  • 10
  • I guess replace the 'or' in ` while(storyChoice != "1"|| storyChoice != "2" || storyChoice != "3") ` with and ie ` while(storyChoice != "1"&& storyChoice != "2" &&storyChoice != "3") ` – advocateofnone May 10 '15 at 21:10
  • Try storyChoice.equals("1") instead of storyChoice == "1" – Suspended May 10 '15 at 21:11
  • 1
    Show me one value for which `x!=1 OR x!=2` will be false. – Pshemo May 10 '15 at 21:11
  • Thank you @Suspended3 , that suggestion seemed to have worked, and my program runs smoothly now. Thank you all for your help! – Sean G May 10 '15 at 21:17
  • What do you mean by "for some reason it isn't picking the users input as a String"? `next()` method returns String, it can't return anything else. Maybe it returns different string than you expect? In that case what values did you expect (and why) and what did you get instead? – Pshemo May 10 '15 at 21:17

0 Answers0