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

  • Thread starter Thread starter JaysFan31
  • Start date Start date
  • Tags Tags
    Method
Click For Summary
SUMMARY

The discussion focuses on creating a Java method named isYorN that checks if a given string parameter is either 'Y' or 'N', regardless of case. The initial implementation using a switch statement is flawed due to variable type mismatches, specifically using a boolean variable incorrectly. A more effective solution involves checking the string's length and converting the character to uppercase for comparison. The final recommended approach is to use str.equalsIgnoreCase() for simplicity and clarity.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with Java methods and return types
  • Knowledge of string manipulation in Java
  • Basic understanding of control flow statements (if, switch)
NEXT STEPS
  • Learn about Java String methods, particularly equalsIgnoreCase()
  • Research Java character manipulation techniques
  • Explore error handling in Java to manage exceptions
  • Study best practices for method design in Java
USEFUL FOR

Beginner Java developers, programming students, and anyone looking to improve their understanding of string handling and method implementation in Java.

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
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.
 
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';
 
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");
 
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 0 ·
Replies
0
Views
680
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K