开发者

how to write a method that translates a boolean into "yes" or "no"

I'm new to java and I"m need to write a method that translates a boolean true or false into a string "yes" or "no开发者_StackOverflow中文版". I'm kinda lost.

public class Book
{
    private String title;
    private String author;
    private String isbn;
    private int pages;
    private boolean pback;
    private double price;

    /**
     * Constructor for objects of class Book
     */
    public Book(String bookTitle, String bookAuthor, String bookCode, int bookPages, boolean paperback, double bookRetail)
    {
        // initialise instance variables
        title = bookTitle;
        author = bookAuthor;
        isbn = bookCode;
        pages = bookPages;
        pback = paperback;
        price = bookRetail;
    }


public String translate(boolean trueorFalse)
            {
                if(pback = true)
                    {
                        ??????;
                    }            
                 else(pback = false)
                    {
                        ???????;
                    } 

            }


boolean myBoolean = true;
String result = myBoolean ? "yes" : "no";


The conditional operator is your friend:

public static String translate(boolean trueOrFalse) {
    return trueOrFalse ? "yes" : "no";
}

In general, if you find yourself writing:

SomeType x;
if (someCondition) {
    x = someExpression;
} else {
    x = someOtherExpression;
}

it's generally nicer to use:

SomeType x = someCondition ? someExpression : someOtherExpression;

The conditional operator makes sure that only one of someExpression or someOtherExpression is evaluated, so you can use method calls etc, confident that they won't be executed inappropriately.

Of course there are times when this gets too complicated - you need to judge the readability of each form for yourself.


There is a project from Apache Group called Apache Commons Lang for working with common Java classes like Boolean. Its BooleanUtils class has some nice methods to work with:

toStringOnOff(boolean bool) - converts a boolean to a String returning 'on' or 'off'
toStringOnOff(Boolean bool) - converts a Boolean to a String returning 'on', 'off' or null
toStringTrueFalse(boolean bool) - converts a boolean to a String returning 'true' or 'false'
toStringTrueFalse(Boolean bool) - converts a Boolean to a String returning 'true', 'false' or null
toStringYesNo(boolean bool) - converts a boolean to a String returning 'yes' or 'no'
toStringYesNo(Boolean bool) - converts a Boolean to a String returning 'yes', 'no' or null

In your example you should work with the toStringYesNo method.

boolean myBoolean = false;
String result = BooleanUtils.toStringYesNo(myBoolean);
System.out.println(result);

This will print

no

To add the library to your project just add it to your Maven pom.xml dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.6</version>
</dependency>


if(pback == true)
{
    return "yes";
} else {
    return "no";
} 

A couple of things to note:

  • equality is tested using ==, so you should write if ( a == b ) , not if ( a = b );
  • returning a value from a method is done using the keyword return followed by the value;
  • else does not take a supplementary argument, unless you want to say else if which then takes an expression similar to if, e.g. else if ( a ==b ).


if (pback) {
    return "yes";
}
else {
    return "no";
}

I feel like I'm missing something.


First, the parameter for your translate method is never used. You should fix that.

Second, do you need to use the String values "Yes" and "No" for conditionals? If not and you can just use the boolean version(and I see no reason that you couldn't) I suggest leaving the boolean.

You can then translate that boolean into "Yes" or "No" when it is outputted using something like the following code:

        //say your boolean variable is called gotIt
            if(gotIt == true) //you can also just say if(gotIt) here
                {
                    //here you place the string where it needs to be, either output it or place it into a variable
                     System.out.println("Yes");
                }            
             else
                {
                    //same as above but for false
                    System.out.println("No");
                } 

        }

The fact is it is much easier to use conditionals with boolean values as opposed to testing 2 strings for equivalence.


The above advices should do the job, but I would recommend you to use:

public String translate(boolean trueOrFalse)
{
    return String.valueOf(trueOrFalse);
}

because later you can easily convert that back:

public boolean translateBack(String translation)
{
    return Boolean.parseBoolean(translation);
}

but the translation string will be "true" of "false" :)


String yesNo(boolean b) {
    String[] s = {"yes", "no"};
    return b ? s[0] : s[1];
}

EDITED with correct return

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜