Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    64
    Rep Power
    0

    Default How to exit a while loop with user input and string comparisons?

    I'm trying to create a while loop that will exit when the user input matches a hard coded string:
    Java Code:
    package randomExperiments;
    
    import java.util.Scanner;
    import java.lang.Double;
    import java.text.NumberFormat;
    import java.lang.Integer;
    
    public class RandomExperiments {
    	
    	public static void main(String[] args){
    				
    		//Comparing Strings
    		String python = "Python";		
    		String guess = "";
    		System.out.println("You're not going anywhere until you guess what the 2nd best programming language in the world is!");
    		while(!guess.equals(python)){
    			//Scanner input = new Scanner(System.in);
    			//guess = input.nextLine();
    			if(guess.equalsIgnoreCase(python)){
    				break;
    			}else{
    				System.out.println("Nope! Guess again.");
    				Scanner input = new Scanner(System.in);
    				guess = input.nextLine();
    			}
    		}
    	}
    }
    But instead of taking in new input, it keeps telling me to guess again, then when I try to enter the guess, the program terminates:
    How to exit  a while loop with user input and string comparisons?-whileloop01.png

    Why does that happen?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,960
    Rep Power
    31

    Default Re: How to exit a while loop with user input and string comparisons?

    Try debugging the code by printing out the contents of guess with the "Nope! Guess again." message so you can see what the computer sees.

    You should only define one instance of the Scanner class outside of the loop.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: How to exit a while loop with user input and string comparisons?

    The program works for me.
    First it prints, "Nope! Guess again."

    After that it continues to loop and print the error message until I enter python.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Real new guy stuck using user input in for loop
    By popeye in forum New To Java
    Replies: 5
    Last Post: 03-21-2014, 01:34 AM
  2. having issue with user input and using while loop
    By barqcider in forum New To Java
    Replies: 4
    Last Post: 10-09-2012, 01:06 AM
  3. Replies: 10
    Last Post: 05-11-2011, 02:26 AM
  4. [SOLVED] User Input - loop
    By new person in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 10:02 PM
  5. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 10:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •