Could anyone point me to why when the user enters Y or N, this loop still continues looping instead of quitting?
Thanks
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.
Could anyone point me to why when the user enters Y or N, this loop still continues looping instead of quitting?
Thanks
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));
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?
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.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
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?
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?
Yes sir. That is what I am looking for.
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.
Well, here is his code:
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.String option = "";
do{
option = ......
} while (!option.equals(whatever));
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.
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.