Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: while loop loops foreve even with the right inputs.

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default while loop loops foreve even with the right inputs.

    Could anyone point me to why when the user enters Y or N, this loop still continues looping instead of quitting?

    Thanks

                             System.out.println("Enter Y to indicate  Up OR N to indicate Down " + "\n");			
    			String option = scanner.nextLine();
     
    			while (!response.equalsIgnoreCase("Y")||(!response.equalsIgnoreCase("N"))) {
    				System.out.println("Enter Y to indicate  Up OR N to indicate Down " + "\n");			
    			        option = scanner.nextLine();
    				}


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: while loop loops foreve even with the right inputs.

    If the user enters N the first part (!response.equalsIgnoreCase("Y")) is true and if the user enters Y the second part is true. If the user enters X both parts are true. Use the && operator

    The normal way of programming is to use a do while loop.
    String option = "";
    do{
      option = ......
    } while (!option.equals(whatever));

  3. #3
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: while loop loops foreve even with the right inputs.

    Hi PhHein,

    I see the logic in it but how could I write it such that only when the user enter's Y or N will the loop stop because based on whether the user enter's Y or N, the programme goes into the next state. So, it is expedient the user enter's one of these options. Anyway, one could still do this and not fall into this logical trap?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: while loop loops foreve even with the right inputs.

    how could I write it such that only when the user enter's Y or N will the loop stop because based on whether the user enter's Y or N
    Whoa! Slow down. Make sense. Communicate clearly. It's not clear you understand the requirement, and if you don't understand it or can't communicate it clearly, then we can't help you.

  5. #5
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: while loop loops foreve even with the right inputs.

    Sorry Greg IF I didn't make sense. Here is what I want to make sense of: I want the user to either choose Y or N for the loop to quite. Should the user enter any other character aside these two, the loop continues. Only when the user enters Y for yes and N for no, should the loop quite. Does that make sense now?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: while loop loops foreve even with the right inputs.

    Here's how I read your explanation:

    User enters:

    'Y' = loop quits (exits)
    'N' = loop quits (exits)
    Any character other than 'Y' or 'N' = loop continues

    Is that what you meant?

  7. #7
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: while loop loops foreve even with the right inputs.

    Yes sir. That is what I am looking for.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: while loop loops foreve even with the right inputs.

    What was the wrong with PhHein's suggestion to use && instead of ||? Did you try that?

    Edit: And why not use 'U' for Up and 'D' for down (or similar)? The Y/N logic to indicate the user's desired state is confusing to the programmer and makes little sense to the user.

  9. #9
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: while loop loops foreve even with the right inputs.

    Well, here is his code:
    String option = "";
    do{
    option = ......
    } while (!option.equals(whatever));
    I didn't quite make much sense out of it. I did ask if he could elaborate more on it. I understood his idea that you can't use the !option.equalsIgnore("Y") when it is N or Y when it is N. What I don't understand is how his do-while loop solves that.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: while loop loops foreve even with the right inputs.

    His response had 2 suggestions, not necessarily meant to be taken together:

    Suggestion 1: Change || to &&

    Suggestion 2: Change the design from a while to a do/while

    His Suggestion 2 indicated the more common approach to what you're trying to do based on the general rule for which loop to choose: If the loop is driven by a known number of occurrences: use for(): if the number of desired loops is unknown, use while(); if the number of desired loops is unknown but at least one execution is required, use do/while(). HOWEVER, any can be used.

    Try his first suggestion and see what happens.

  11. #11
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: while loop loops foreve even with the right inputs.

    Hi, Greg,

    I solved it now. I just had to think about it differently because I didn't understand his approach. Thanks anyways for the response.

Similar Threads

  1. Replies: 6
    Last Post: March 12th, 2014, 12:43 PM
  2. Why won't my program compile? (While loop with inputs)
    By LWorm in forum Loops & Control Statements
    Replies: 5
    Last Post: October 14th, 2013, 10:52 PM
  3. Replies: 6
    Last Post: October 3rd, 2012, 06:39 PM
  4. Concatenating Inputs, need help please!
    By xdrechsler in forum File I/O & Other I/O Streams
    Replies: 17
    Last Post: August 19th, 2011, 01:02 PM
  5. Inputs not being applied.
    By Norflok669 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 01:25 AM

Tags for this Thread