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

  • Context: Java 
  • Thread starter Thread starter JaysFan31
  • Start date Start date
  • Tags Tags
    Method
Click For Summary

Discussion Overview

The discussion revolves around creating a Java method that checks if a given string is either "Y" or "N" (case insensitive). Participants explore different approaches to implement this functionality, including the use of switch statements, character checks, and string comparisons.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant proposes a method using a switch statement to check for 'y', 'Y', 'n', and 'N', but encounters compilation errors related to variable types.
  • Another participant suggests that a switch statement is suitable for single characters and recommends looping through an array to test each character.
  • A different viewpoint advises against using switch statements and suggests first checking if the string length is 1, returning false if not, and then comparing the character to 'N' or 'Y' after converting it to uppercase.
  • A later reply corrects a potential typo in the character comparison and offers a simpler approach using string equality checks for both uppercase and lowercase letters.

Areas of Agreement / Disagreement

Participants express differing opinions on the best method to implement the functionality, with no consensus reached on a single approach. Some favor using switch statements while others advocate for string comparisons or character checks.

Contextual Notes

Some participants mention issues with variable types and string length checks, indicating potential limitations in the proposed solutions. There is also a mention of a typo in the character comparison that could lead to confusion.

Who May Find This Useful

Beginners in Java programming, particularly those interested in string manipulation and conditional logic.

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");
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · 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
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K