1

How can we get the boolean expression of a basic singleton with hashsets to return a message in lieu of the original 'true' or 'false'?

public boolean bookLane(String lane) {
    return runLane.remove(lane);
}

I want to only replace the true or false return statements with a message.

To help clarify this question, something like below (i know it doesn't work) is the direction I am wanting to go...

public boolean bookLane(String lane) {
    if (true)
    {
        message = "Lane is available. Adding runner...";
        //instead of true
    }
    else
    {
        message = "Lane unavailable. Please choose another";
        //instead of false
    }
    return runLane.remove(lane);
}

I just tried messing with the code and found that it only returns false now.

public boolean bookLane(String lane) {
    String message1 = "Lane Available. Adding runner...";
    String message2 = "Lane is Unavailable.";
    if (runLane.remove(lane))
    {
        System.out.println(message1);
    }
    else
    {
        System.out.println(message2);
    }
    return runLane.remove(lane);//
}

Any ideas on fixing it? Not gonna lie, my experience with Java is mainly trial and error with a little help from more experienced programmers. I think this method could work if someone could let me know what I am missing as far as how boolean methods work with more than just the one return type. I am trying to target the return value for displaying the appropriate message with the returned boolean. Is this route even possible? Am I missing some logic in how boolean methods work or something? Please understand my frustration and my need for yalls help. thanks for the guidance given...

  • 2
    A method returning a boolean can only return a boolean. Not a String. If you want to return a message, then change the return type to String. But really, the caller of the method should use the boolean returned value to choose a message and, I guess, display it. – JB Nizet Aug 29 '15 at 07:58
  • As @JBNizet wrote: change return type from `boolean` to `String`. Then assign `runLane.remove(lane)` to a local variable at first and use it for your logic to pick the message to return. – MirMasej Aug 29 '15 at 08:11
  • Could this be a duplicate of http://stackoverflow.com/questions/7470861/return-multiple-values-from-a-java-method-why-no-n-tuple-objects ? – Aleksandr Erokhin Aug 29 '15 at 10:50
  • @AlexErohin that seems more like a debate than anything else. I like cutting straight to the chase. there are no other posts like this. – SYS_DESTROY Aug 29 '15 at 19:23
  • Are you saying that in your updated code you can't get the `true` case even if you remove the lane which is in `runLane`? To check it do `System.out.println("contains? " + runLane.contains(lane);` . If it prints `contains? true` and still `message1` isn't printed, that would be strange at least. – MirMasej Sep 01 '15 at 18:19

2 Answers2

0

In these cases it might be useful to define a new data structure, since you want to retrieve two pieces of information from the same method. Consider the following:

class BookLaneResult {
    boolean success;
    String message;
    // add constructors / getters / other stuff you need
}

Then your code becomes this:

public BookLaneResult bookLane(String lane) {
    // some logic to determine if lane is available or not
    boolean laneAvaiable = ...;

    return new BookLaneResult(laneAvailable, laneAvailable ? "Lane available" : "Lane unavailable");
}

If the BookLaneResult is to be used only in that case with only those messages, then you can just use a boolean parameter constructor and set the message based on the argument internally. However, to make the new data structure more flexible you can name it OperationResult and use it whenever you perform some kind of operation and expect to retrieve a boolean flag representing the operation success or failure and the message stating what happened.

AlmasB
  • 3,377
  • 2
  • 15
  • 17
0

Here is what I used for a boolean operation trying to figure out if a character is a letter - if it is a letter, true, false if no.

Code

boolean isLetter = Character.isLetter(ch); 
    if (isLetter == true) {
        Boolean.toString(isLetter); 
        System.out.println(" This is a letter."); }
    else  {
        Boolean.toString(isLetter);
        System.out.println(" This is not a letter."); }

Worked for me - might be able to apply to other boolean operations.

Output

Input data: 123456abcdef
1  2 This is not a letter
2  3 This is not a letter
3  4 This is not a letter
4  5 This is not a letter
5  6 This is not a letter
6  7 This is not a letter
a  b This is a letter
b  c This is a letter
c  d This is a letter
d  e This is a letter
e  f This is a letter
f  g This is a letter

I realize this is years late, but including this for those who pop up on this thread from a Google search.

Sam R
  • 1
  • 2