• Post Reply Bookmark Topic Watch Topic
  • New Topic

YesNoChecker  RSS feed

 
kendra spanzy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was tasked with writing a program that prints the question ”Do you want to continue?” and reads a
user input. If the user input is ”Y”, ”Yes”, ”OK”, ”Sure”, or ”Why not?”, print out ”OK”. If
the user input is ”N” or ”No”, then print out ”Terminating”. Otherwise, print ”Bad input”.
The case of the user input should not matter. For example, ”y” or ”yes” are also valid
inputs.

Write a class YesNoChecker for this purpose

i made a class continue



now comes the problem im facing i dont know how to get started on the methods .isYes and .isNo and how to ignore letter cases . please be kind enough to give me some guidance thank you

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

How are you learning Java? Has nobody taught you how to write a method? Start in the Java Tutorials.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way to ignore case is to convert all strings involved in the comparison to lower (or upper) case before comparing them.
Although, if you choose a solution that involves direct comparison of strings, there is a method in the String class that allows you to compare two strings whilst ignoring their case.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:The easiest way to ignore case is to convert all strings involved in the comparison to lower (or upper) case before comparing them.
Although, if you choose a solution that involves direct comparison of strings, there is a method in the String class that allows you to compare two strings whilst ignoring their case.

But be careful with that when you compare strings that contains some locale specific characters.
For example:This prints:
ß
SS

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought the equalsIgnoreCase method would be easier than an explicit conversion. Joanne.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would have thought the equalsIgnoreCase method would be easier than an explicit conversion. Joanne.

I'm not sure that it handles the sort of cases that Pawel mentioned though (eg, the Turkish "i"), but if it doesn't need to...

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would have thought the equalsIgnoreCase method would be easier than an explicit conversion. Joanne.

Yes, if you were directly comparing Strings, which is why I mentioned it, but I was thinking of a more scalable solution where you just add all the valid strings to suitable collections and then check if the entered string is contained in either of the collections.
You would have to make all the strings in the collections lower (or upper) case as there isn't a containsIgnoreCase method.
Doing it this way makes it easier to add new valid strings - just add them to the collection initialisation code rather than having to extend your if/else/if block of code.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The implementation uses enum types for validation. Using a static method in the YesNoChecker class you pass in the response as a string, use conditional logic to determine what enum type to return.
In the Continue class you use a switch statement and the static method of the YesNoChecker class to determine the output.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good grief! returning ints as options like that
Surely those methods would have a boolean return type, or if you really need three options, you would return elements from an enumerated type.
I would put handling of incorrect input inside the get input method.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Good grief! returning ints as options like that
Surely those methods would have a boolean return type, or if you really need three options, you would return elements from an enumerated type.
I would put handling of incorrect input inside the get input method.


Sorry for the whacky implementation that you are bashing which was provided earlier. I had already started typing a solution and had to leave abrupt so I just decided to click submit.

I've edited the code to provide your recommendations.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, don't change posts like that after replies; it makes the replies look like nonsense.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!