Welcome to Dream.In.Code
Become a Java Expert!

Join 149,516 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,377 people online right now. Registration is fast and FREE... Join Now!




help with a method

 
Reply to this topicStart new topic

help with a method

PvtBillPilgrim
23 May, 2007 - 12:31 PM
Post #1

New D.I.C Head
*

Joined: 7 May, 2007
Posts: 21


My Contributions
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:
CODE

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.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Help With A Method
23 May, 2007 - 01:26 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
i don't quite understand the problem... If you were to declare str as a char instead of a string (since you are expecting only 1 letter). You could place it in the switch statement directly.
A switch statement is based on an integer, which a boolean is as well, false is 0 and true is 1.
CODE

public static boolean isYorN(char str)
{
switch (str)
{

all of your lines which assign the variable character, will not do anything, since the value of character needs to be determined before ever entering the switch statement, as well the variables scope ends after the return statement, making the assignment useless. You also do not require the break statements inside of the switch, in most cases they are necessary, but since you are returning a value in all cases, the break statement of each case is never reached, and unnecessary.
User is offlineProfile CardPM
+Quote Post

Mila
RE: Help With A Method
23 May, 2007 - 01:42 PM
Post #3

D.I.C Head
Group Icon

Joined: 28 Oct, 2006
Posts: 94


Dream Kudos: 25
My Contributions
You could always use String's charAt method to get the first character from the string.

Like this:
CODE
public static boolean isYorN(String str)
{
boolean character = str.charAt(0);
if (character == 'y' || character == 'Y')
return true;

if (character == 'n' || character == 'N')
return true;

return false;
}

User is offlineProfile CardPM
+Quote Post

keems21
RE: Help With A Method
23 May, 2007 - 04:27 PM
Post #4

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
Well let me confuse you a little bit more. Because Strings are Objects, they have methods that will allow you to do this checking that you want.

The one method that will do you wonders in this case is the equalsIgnoreCase() method. You can check it out in the Java api yourself if you want:
http://java.sun.com/j2se/1.5.0/docs/api/ja...ang/String.html

Because this method already returns a boolean value, you can use it directly in your return statement.

This may come off as a little confusing initially, but just bear with it:
CODE
public static boolean isYorN(String str)
{
  return str.equalsIgnoreCase("y") || str.equalsIgnoreCase("n");
}


When the code:
str.equalsIgnoreCase("y")
is called, it is comparing whatever value you have in the variable str, and it is trying to determine if it is equal to "y" or "Y". If neither of these are correct, then the statement after the or operator (this thing -> ||) is read:
str.equalsIgnoreCase("n")
in the same way, this code determines whether the value in str is either "n" or "N".

Because both of these values return a boolean as a result, if the value in the variable str is "y", "Y", "n", or "N", then it will automatically return ture. Otherwise, the method will return false.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:10PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month