Another one .......

(i) Write a method called isYorN 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.

my code

// This is method (i)

publicstaticboolean isYorN ( String letter )

{

boolean result;

if ( letter =="Y" || letter =="N" || letter =="y" || letter=="n" )

{ result =true;}

else

{ result =false;}

return result;

}

[1095 byte] By [Ace1a] at [2007-11-27 5:17:09]
# 1
I know what's wrong, but rather than give you the answer, I'm going to ask you to describe your problem first. It's a good habit to get into.
jverda at 2007-7-12 10:40:04 > top of Java-index,Java Essentials,Java Programming...
# 2
Nope.
cotton.ma at 2007-7-12 10:40:04 > top of Java-index,Java Essentials,Java Programming...
# 3

Don't use == to compare Strings.

If you use == it will compare addresses.

You want to compare the actual string's themselves so you'll use the

String s1;

String s2;

if (s1.equals(s2))

return true;

lethalwirea at 2007-7-12 10:40:04 > top of Java-index,Java Essentials,Java Programming...
# 4
== tests for object equality (it is the same object).equals tests a string for string equality.Also, String's .equalsIgnoreCase() might come in handy too
xiarcela at 2007-7-12 10:40:04 > top of Java-index,Java Essentials,Java Programming...
# 5
> Nope. http://java.sun.com/mailers/techtips/corejava/2006/tt0822.html#2
cotton.ma at 2007-7-12 10:40:04 > top of Java-index,Java Essentials,Java Programming...
# 6
Also see this reply for a tip on reducing code bloat: http://forum.java.sun.com/thread.jspa?threadID=5175756&start=5
Hippolytea at 2007-7-12 10:40:04 > top of Java-index,Java Essentials,Java Programming...
# 7

Since you posted an attempt I'll post the way I would do it.

public static boolean isYorN(String in)

{

final String YN = "yYnN" ;

if(YN.indexOf(in) > -1){

return true ;

}

return false ;

}

PS.

Message was edited by:

puckstopper31

puckstopper31a at 2007-7-12 10:40:04 > top of Java-index,Java Essentials,Java Programming...
# 8

> Since you posted an attempt I'll post the way I would

> do it.

>

> [code]

>public static boolean isYorN(String in)

> {

> final String YN = "yYnN" ;

> if(YN.indexOf(in) > -1){

> return true ;

>

> return false ;

> /code]

>

> PS.

>

> Message was edited by:

> puckstopper31

Except:

1) return YN.indexOf(in) > -1; is more concise.

2) This allows yY, yYn, yYnN, Yn, YnN, and nN, which I believe goes against the requirements.

jverda at 2007-7-12 10:40:04 > top of Java-index,Java Essentials,Java Programming...
# 9

> Since you posted an attempt I'll post the way I would

> do it.

>

>public static boolean isYorN(String in)

> {

> final String YN = "yYnN" ;

> if(YN.indexOf(in) > -1){

> return true ;

>

> return false ;

isYorN("Yn') is true

isYorN("yn') is false

Hmmm...

Hippolytea at 2007-7-12 10:40:04 > top of Java-index,Java Essentials,Java Programming...
# 10
Pretty easy fix there. Just need to implement the check that "in" is only 1 character.
bheilersa at 2007-7-12 10:40:05 > top of Java-index,Java Essentials,Java Programming...
# 11
> 1) return YN.indexOf(in) > -1; is more concise.I agree, unless he needs to debug. Easier to - later on - insert a print statement after the "work" but before the return, if he assigns to a boolean "result".
bheilersa at 2007-7-12 10:40:05 > top of Java-index,Java Essentials,Java Programming...
# 12

> > 1) return YN.indexOf(in) > -1; is more concise.

>

> I agree, unless he needs to debug. Easier to - later

> on - insert a print statement after the "work" but

> before the return, if he assigns to a boolean

> "result".

Agreed.

I'd still forgo the if though.

boolean result = YN.indexOf(in) > -1;

// print

return result;

jverda at 2007-7-12 10:40:05 > top of Java-index,Java Essentials,Java Programming...
# 13

or..

public static boolean charIsYN(char c)

{

boolean ret=false;

if (c=='Y' || c=='y' || c=='N' || c=='n') {ret=true;}

return ret;

}

public static boolean stringIsYN(String s)

{

boolean ret=false;

if (s!= null && s.length>=1) {

ret=charIsYN(s.charAt(0);

}

return ret;

}

xiarcela at 2007-7-12 10:40:05 > top of Java-index,Java Essentials,Java Programming...
# 14

> or..

> > public static boolean charIsYN(char c)

> {

> boolean ret=false;

> if (c=='Y' || c=='y' || c=='N' || c=='n')

> {ret=true;}

>

> return ret;

>

> }

>

>

> public static boolean stringIsYN(String s)

> {

>

> boolean ret=false;

> if (s.length>=1) {

>ret=charIsYN(s.charAt(0);

> return ret;

> }

>

>

>

Why declare a variable for the result? I would just return when I know what to return.

Kaj

kajbja at 2007-7-12 10:40:05 > top of Java-index,Java Essentials,Java Programming...
# 15

Actually, second method should be this:

public static boolean stringIsYN(String s)

{

boolean ret=false;

if (s!= null && s.length()>=1) {

ret=charIsYN(s.charAt(0));

}

return ret;

}

xiarcela at 2007-7-21 21:23:35 > top of Java-index,Java Essentials,Java Programming...
# 16
I like reply #12's style over reply #13's. Why introduce an if, when you don't need to?
Hippolytea at 2007-7-21 21:23:35 > top of Java-index,Java Essentials,Java Programming...
# 17

variable for the result? I would just

> return when I know what to return.

>

> Kaj

such as

public static boolean charIsYN(char c)

{

if (c=='Y' || c=='y' || c=='N' || c=='n')

{

return true;

}

return false;

}

I was trying to adhere to the "1 return from a method" principle that I hear so often..

xiarcela at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 18

Even shorter... with null checking as a bonus!

public static boolean IsYOrN(String yn) {

return (yn != null && yn.length() == 1 && ("YyNn").indexOf(yn) >= 0 );

}

Message was edited by:

kevjava [Edit: Reply 15 beat me to the whole null-chekcing thing...]

kevjavaa at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 19
As long as we're pounding the krap out of this, I'd prefer contains over indexOf -- more to the point of what's required.
Hippolytea at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 20
> I'd prefer contains over indexOf -- more to the point of what's required.Indeed.
kevjavaa at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 21

> As long as we're pounding the krap out of this, I'd

> prefer contains over indexOf -- more to the point of

> what's required.

But still alows for more than just the single char, unless you have a separate length==1 test.

I'd say it's better to just equals() or equalsIgnoreCase().

jverda at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 22
Once it's a 'char', can't we use it in a switch statement?That would be fun--> and then we could put it in an applet on the internet and answer another post I saw here last week -grin-Dave
xiarcela at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 23
The truly wonderful thing about what we do is that there is almost always more than one way to skin a cat and most likely all of them have value. Certainly some will be stronger than others but they all likely have some merit to them.PS.
puckstopper31a at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 24

> I was trying to adhere to the "1 return from a

> method" principle that I hear so often..

I'm still trying to tell people to stop listening to that.

I don't know why people are doing that, but I think that it is an inheritance from C/C++ where you don't have a finally clause.

I always follow the princible of returning as soon as possible. Why read more code than you need to? It does also lead to less nesting.

Kaj

kajbja at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 25
[url #" style="display: block; background-image: url(' http://www.brandtarot.com/blog/wp-content/uploads/2006/09/skin_a_cat.jpg'); width: 432px; height: 416px] [/url]
Hippolytea at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 26

> Even shorter... with null checking as a bonus!

>

> [code]

>public static boolean IsYOrN(String yn) {

> return (yn != null && yn.length() == 1 &&

> ("YyNn").indexOf(yn) >= 0 );

>}

> ode]

>

> Message was edited by:

> kevjava [Edit: Reply 15 beat me to the whole

> null-chekcing thing...]

Allthough I personally prefer this to what I wrote. I have run into teams that considered terniary operations to be anathema. Don't ask me why I never did get the straight of it. I think they just couldn't make sense out of it so of course it's bad. I will concede that it can be harder to read than a block if.

PS.

puckstopper31a at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 27

> Allthough I personally prefer this to what I wrote. I

> have run into teams that considered terniary

> operations to be anathema. Don't ask me why I never

> did get the straight of it. I think they just

> couldn't make sense out of it so of course it's bad.

> I will concede that it can be harder to read than a

> block if.

I certainly wouldn't use the ternary here.

boolean result = a ? b : !b;

can always simply be

boolean result = a;

or

boolean result = !a;

depending on the "sign" of b.

jverda at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 28
I'm rather of the mindset, "Whatever works" here. I am a fan of graceful readable code. Which is a fairly ambiguous criteria largely dependent on the taste of the person evaluating for graceful/readable.PS.
puckstopper31a at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 29

> I'm rather of the mindset, "Whatever works" here. I

> am a fan of graceful readable code. Which is a fairly

> ambiguous criteria largely dependent on the taste of

> the person evaluating for graceful/readable.

>

> PS.

I agree. I just happen to think that using a more complex structure that adds nothing functionally had a negative impact on readability.

jverda at 2007-7-21 21:23:36 > top of Java-index,Java Essentials,Java Programming...
# 30

I would functionally agree with that. The only caveat is that what is readable and makes sense to me might be incomprehensible to you through no deficiency on either of our parts just a difference in coding dialect.

As an example, the team I'm with these days has two conventions that are very difficult for me to adhere to.

public class MyClass

{

.... stufff

public void myMethod()

{

....

}

}

opening braces on the line following the scope declaration. To me

public class MyClass{

... suff

public void myMethod(){

.... stuff

}

}

is much more readable. Which is right? Both it's just a difference in coding dialect.

The other one is they like underscores at the beginning of private members.

private String _myString

Just remembering to underscore things requires braincells and I don't have that many to spare.

PS.

}

puckstopper31a at 2007-7-21 21:23:41 > top of Java-index,Java Essentials,Java Programming...
# 31

Was something like this mentioned yet...

public static boolean isYN(String toEval){

return ("Y".equalsIgnoreCase(toEval)||"N".equalsIgnoreCase(toEval));

}

Null testing included no charge.

public class Test{

public static boolean isYN(String toEval){

return ("Y".equalsIgnoreCase(toEval)||"N".equalsIgnoreCase(toEval));

}

public static void main(String[] args){

String[] samples = {"y","n","Y","y","YN","a",null};

for(int i=0;i<samples.length;i++){

System.out.println("Testing '"+samples[i]+"' result = "+Test.isYN(samples[i]));

}

}

}

">

cotton.ma at 2007-7-21 21:23:41 > top of Java-index,Java Essentials,Java Programming...
# 32

> Was something like this mentioned yet...

>

> public static boolean isYN(String toEval){

> return

> ("Y".equalsIgnoreCase(toEval)||"N".equalsIgnoreCase(t

> Eval));

>}

Hinted at but code not provided, I believe.

This is probably how I'd do it.

jverda at 2007-7-21 21:23:41 > top of Java-index,Java Essentials,Java Programming...
# 33

> Hinted at but code not provided, I believe.

>

Yeah I saw equalsIgnoreCase mentioned but then someone else mentioned nulls.

I guess for comparison sake (not for you obviously but perhaps the OP or other later readers).

public class Test{

public static boolean isYN(String toEval){

return ("Y".equalsIgnoreCase(toEval)||"N".equalsIgnoreCase(toEval));

}

public static boolean notSoGoodIsYN(String toEval){

return (toEval.equalsIgnoreCase("Y")||toEval.equalsIgnoreCase("N"));

}

public static void main(String[] args){

String[] samples = {"y","n","Y","y","YN","a",null};

for(int i=0;i<samples.length;i++){

System.out.println("Testing '"+samples[i]+"' result = "+Test.isYN(samples[i]));

}

System.out.println("testing with not so good");

for(int i=0;i<samples.length;i++){

System.out.println("Testing '"+samples[i]+"' result = "+Test.notSoGoodIsYN(samples[i]));

}

}

}

Note the difference in output.

> This is probably how I'd do it.

Well that's because you're a smart guy. ;)">

cotton.ma at 2007-7-21 21:23:41 > top of Java-index,Java Essentials,Java Programming...
# 34

> variable for the result? I would just

> > return when I know what to return.

> >

> > Kaj

>

> such as

>

> > public static boolean charIsYN(char c)

> {

>

> if (c=='Y' || c=='y' || c=='N' || c=='n')

>{

> return true;

> }

>

> return false;

> }

>

>

> I was trying to adhere to the "1 return from a

> method" principle that I hear so often..

or rather

public static boolean charIsYN(char c){

return c=='Y' || c=='y' || c=='N' || c=='n';

}

anu_sh3a at 2007-7-21 21:23:41 > top of Java-index,Java Essentials,Java Programming...