- #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.
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.