Forums Register Login

Comparing String and Char values with a NOT operator

+Pie Number of slices to send: Send
Hello i was having trouble comparing a two string and char values. What i am trying to do is if the user enters a value that is not the correct one, the JOption pane will keep asking the user to enter a correct value. Here is an example:


On this one I ask the user to enter a number between 10-24 and if the input is not within the range i asked the user, the while statement will keep asking the user to input a valid number. However, i couldn't figure out how to do this with char values. This is the best i could do but this is still not correct because i need the program to keep asking the user to input a valid letter in a while loop.
 

I got another program that needs to keep asking the user to input a valid answer but this time with Strings. This is also the best i could do. Now this time this program runs fine but it doesn't give me the correct outcome.
I made the program to ask the user to input "Yes" or "No" and i added a while loop so that if the user dosent input the correct answer, the while loop will keep asking the user to input a correct one. Furthermore, i want the program to ignore cases, so it dosn't matter if they input YES, yes, Yes or no, NO, No and so on. So i tried making the program and as i said it looks fine and runs but when i input the corrext input that i want, it goes to the while loop that was meant for invalid answers when in reality it is correct. Does anyone know how to fix these 2 programs?



And thank you
+Pie Number of slices to send: Send
The negation of "Yes or No" is not "Not yes or not no" but "Not yes and not no" ( !(a||b) <-> !a&&!b ).
+Pie Number of slices to send: Send
What? On which code are you talking about? Can you post the upgraded code?
+Pie Number of slices to send: Send
Ooooo I think i found out the problem to the third code i posted. This is what i did in the while loop:

 

It seems that the NOT (!) operator had to be outside of the conditional statements instead by the "input.equalsIgnoreCase(y)" part. Now that i put the NOT operator in this position i am now getting the results i need.
+Pie Number of slices to send: Send
 

Tobias Bachert wrote:The negation of "Yes or No" is not "Not yes or not no" but "Not yes and not no" ( !(a||b) <-> !a&&!b ).

 

I found out what you meant now Tobias! thanks
+Pie Number of slices to send: Send
If you want to look up more about this logic rule, it's one of De Morgan's laws.
+Pie Number of slices to send: Send
Avoid System.exit; it appears all right in a simple example like yours, but can cause dangerous problems in more complicated programs.
Why are you using option panes for keyboard input? That is very old‑fashioned programming style.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Avoid System.exit; it appears all right in a simple example like yours, but can cause dangerous problems in more complicated programs.
Why are you using option panes for keyboard input? That is very old‑fashioned programming style.



Because i am taking a beginner java programming course and as any beginner textbook, it starts off with the basics.
+Pie Number of slices to send: Send
There is nothing basic about option pane for keyboard input. The basic method of keyboard input has been Scanner for the last twelve years. When was your book printed?
+Pie Number of slices to send: Send
Its "Starting out with JAVA From control structures through objects. Published 2016
+Pie Number of slices to send: Send
However it did start off with the Scanner class then the JOptionPane but you asked why am i using such an old technique for input. I answered that i am in the beginner stages of java and hence, why i am using JOptionPane and not the input you were thinking
+Pie Number of slices to send: Send
 

jonny Alva wrote:Its "Starting out with JAVA From control structures through objects. Published 2016

This, I presume.
+Pie Number of slices to send: Send
 

jonny Alva wrote:


1.
Please explain, why are you giving user so many choices instead of just: "y for Yes, n for No"?
Would there be difference if user would type 'Y' instead of 'y'?

2.
Try to get good code formatting habits. These are important.
  • Always place space character before {
  • Always put space characters around +, -, =, *, /, <, >, ||, &&, |, &, %

  • Thinking that it isn't important, and ignoring it, it makes your code look very narrowed, which makes it difficult to read.

    3. Have you been taught about methods yet?
    +Pie Number of slices to send: Send
    You might find the String#charAt() method useful. Pass 0 as its argument and you get the first character, and you can test for equality to 'y' or 'n'. Using toLowerCase() on the String first may make things easier: . . . myString.toLowerCase().charAt(0)
    My, my, aren't you a big fella. Here, have a tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1104 times.
    Similar Threads
    while loop stuck
    How to compare String and int?
    Run Time Error
    Only 2 decimal places!!!!
    Valadation problem!!!!!!
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 13:26:36.