Reputation: 159
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". 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)
{
???????;
}
}
Upvotes: 8
Views: 45051
Reputation: 493
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>
Upvotes: 9
Reputation: 41097
String yesNo(boolean b) {
String[] s = {"yes", "no"};
return b ? s[0] : s[1];
}
EDITED with correct return
Upvotes: -5
Reputation: 897
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.
Upvotes: 0
Reputation: 10639
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" :)
Upvotes: -1
Reputation: 22116
if (pback) {
return "yes";
}
else {
return "no";
}
I feel like I'm missing something.
Upvotes: 1
Reputation: 1500225
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.
Upvotes: 27
Reputation: 39733
boolean myBoolean = true;
String result = myBoolean ? "yes" : "no";
Upvotes: 36
Reputation: 68268
if(pback == true)
{
return "yes";
} else {
return "no";
}
A couple of things to note:
==
, so you should write if ( a == b )
, not if ( a = b )
;return
followed by the value;else if
which then takes an expression similar to if
, e.g. else if ( a ==b )
.Upvotes: 4