Java Method for Checking Y or N: A Beginner's Guide

  • Java
  • Thread starter JaysFan31
  • Start date
  • Tags
    Method
In summary, the conversation is about creating a method in Java that takes a String as a parameter and returns "true" if the parameter is the letter Y or N (in either upper or lower case), or false otherwise. The use of switch statements is suggested, but the speaker is struggling with incompatible types and is looking for other solutions such as looping through an array. Another approach is suggested, checking the length of the string and then comparing it to the characters N and Y. The conversation ends with a discussion on the most efficient way to solve the problem.
  • #1
JaysFan31
I'm very new to Java and I was looking for some help on this particular problem.

I need to create a method that takes a String as a parameter and returns "true" if the parameter is the letter Y or the letter N (in either upper or lower case), or false otherwise.

Most of the methods we've been working on are numbers (integers mostly) not strings, so this one confuses me quite a bit. How do you make the computer look for a certain character like y, Y, n, N. I don't want to declare them as variables, I just want Java to see them as letters.

I have:
public static boolean isYorN(String str)
{
boolean character;
switch (character)
{
case 'y':
character = true;
return true;
break;

case 'Y':
character = true;
return true;
break;

case 'n':
character = true;
return true;
break;

case 'N':
character = true;
return true;
break;

default:
character = false;
return false;

}
}
}

I think a switch case is the best method, but I'm missing something about naming because all my compiling errors are telling me that I have incompatible types (i.e. found: boolean, required: int).

Thank you in advance for any help.
 
Technology news on Phys.org
  • #2
The switch would be the best method if you were looking at a single character.
I would try looping through an array testing each character.
 
  • #3
Well, I would stay away from switch statements if possible.

I would first check if the String(bottom line it is sequence(array) of chars) is of length 1 if not return false.

if(str == null || str.length != 1){
return false;
}

now since the string is of length 1, let's get the char and see if it is N or Y.

char chr = Charachter.toUpperCase(str.getCharAt(0));

return char == 'N' || char == 'Y';
 
  • #4
haki said:
char chr = Charachter.toUpperCase(str.getCharAt(0));

return char == 'N' || char == 'Y';

I'm sure you meant return chr == 'N' || chr == 'Y';

I think haki's way is the smartest here, but as the most basic you could also just do:

return str.equals("Y") || str.equals("y") || str.equals("N") || str.equals("n");
 

FAQ: Java Method for Checking Y or N: A Beginner's Guide

What is a Java method for checking Y or N?

A Java method for checking Y or N is a function that evaluates a user's input and determines if it is a "Yes" or "No" response. This method typically uses conditional statements to compare the input to predefined values.

How do I create a Java method for checking Y or N?

To create a Java method for checking Y or N, you will need to define a method with a return type of boolean. Within the method, you can use conditional statements such as if/else or switch/case to compare the input to the expected values. Finally, the method should return either true or false depending on the input.

Can I use a Java method for checking Y or N in any program?

Yes, you can use a Java method for checking Y or N in any program as long as you have defined the method correctly and call it in the appropriate place within your code. It is a useful tool for validating user input in various types of applications.

What happens if the user enters an invalid input when using a Java method for checking Y or N?

If the user enters an invalid input, the method will return false or an error may be thrown, depending on how you have defined the method. It is important to handle invalid inputs in your code to provide a smooth user experience.

Are there any other methods for checking Y or N besides using conditional statements?

Yes, there are other methods for checking Y or N such as using regular expressions or creating a custom class. However, using conditional statements is the most straightforward and beginner-friendly approach.

Similar threads

Replies
13
Views
4K
Replies
3
Views
3K
Replies
9
Views
2K
Replies
12
Views
1K
Replies
7
Views
2K
Replies
1
Views
2K
Back
Top