Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing String and Char values with a NOT operator  RSS feed

 
jonny Alva
Greenhorn
Posts: 14
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tobias Bachert
Ranch Hand
Posts: 85
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The negation of "Yes or No" is not "Not yes or not no" but "Not yes and not no" ( !(a||b) <-> !a&&!b ).
 
jonny Alva
Greenhorn
Posts: 14
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What? On which code are you talking about? Can you post the upgraded code?
 
jonny Alva
Greenhorn
Posts: 14
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jonny Alva
Greenhorn
Posts: 14
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Saloon Keeper
Posts: 7807
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to look up more about this logic rule, it's one of De Morgan's laws.
 
Campbell Ritchie
Marshal
Posts: 55707
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jonny Alva
Greenhorn
Posts: 14
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 55707
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
jonny Alva
Greenhorn
Posts: 14
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its "Starting out with JAVA From control structures through objects. Published 2016
 
jonny Alva
Greenhorn
Posts: 14
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 55707
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jonny Alva wrote:Its "Starting out with JAVA From control structures through objects. Published 2016
This, I presume.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
     
    Campbell Ritchie
    Marshal
    Posts: 55707
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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)
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!