How to write a method that translates a boolean value to yes or no,

I am new to java and I need to write a method that translates a boolean value of true or false to the string "yes" or "no". I'm a little 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)
                    {
                        ???????;
                    } 

            }
+5
source share
8 answers
boolean myBoolean = true;
String result = myBoolean ? "yes" : "no";
+31
source

The 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 is usually better to use:

SomeType x = someCondition ? someExpression : someOtherExpression;

The conditional statement ensures that only one of someExpressionor is evaluated someOtherExpression, so you can use method calls, etc., assured that they will not be executed improperly.

, , - .

+24

Apache Group, Apache Commons Lang Java, Boolean. BooleanUtils :

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

toStringYesNo.

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

no

, Maven pom.xml :

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.6</version>
</dependency>
+4
if(pback == true)
{
    return "yes";
} else {
    return "no";
} 

:

  • ==, if ( a == b ), if ( a = b );
  • return, ;
  • else , else if, , if, . else if ( a ==b ).
+3
if (pback) {
    return "yes";
}
else {
    return "no";
}

, - .

+1

-, . .

-, String "" "" ? , ( , ), .

"" "", :

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

        }

, , 2 .

0

, :

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

:

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

"true" "false":)

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

-5

All Articles