How do I print out the result of a method? I want to print out the return of translate but it displays true or false. Suggestions please.
/**
* @return
System.out.println("Paperback: " + translate(pback));
public void displaybook()
{
System.out.println("Paperback: " + translate(pback));
}
public String translate(boolean trueOrFalse) {
if(pback == true) ...
Should probably be:
public String translate(boolean trueOrFalse) {
if(trueOrFalse) ...
It looks like you mistakenly concatenated your variable pback
instead of the result of your translate method in the following statement:
System.out.println("Paperback: " + pback);
Instead, replace that statement with
System.out.println("Paperback: " + translate(pback));
Please don't forget to call the method, since you wrote it for some reason, I guess.
System.out.println("Paperback: " + translate(pback));
Now, few suggestions, do yourself a favour and change the method like below. if(pback == true)
, makes no sense. See, The Test of Truth, for your amusement.
public String translate(boolean pback) {
return pback ? "yes" : "no";
}
Well, if you don't like ternary, do this,
public String translate(boolean pback) {
if(pback) return "yes";
else return "no";
}
If you like braces, put braces there,
public String translate(boolean pback) {
if(pback) {
return "yes";
} else {
return "no";
}
}
If you don't like 2 return statements, do this,
public String translate(boolean pback) {
String yesNo;
if(pback) {
yesNo = "yes";
} else {
yesNo = "no";
}
return yesNo;
}