简体   繁体   English

java单例布尔表达式返回消息而不是true或false

[英]java singleton boolean expression to return message instead of true or false

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.我只想用消息替换 true 或 false 返回语句。

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.我只是尝试弄乱代码,发现它现在只返回 false。

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.不会撒谎,我在 Java 方面的经验主要是在更有经验的程序员的帮助下反复试验。 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...感谢给予的指导...

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.如果BookLaneResult仅在那种情况下仅用于那些消息,那么您可以只使用boolean参数构造函数并在内部根据参数设置消息。 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.但是,为了使新的数据结构更加灵活,您可以将其命名为OperationResult并在您执行某种操作时使用它,并希望检索表示操作成功或失败的布尔标志以及说明所发生情况的消息。

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.我意识到这已经晚了很多年,但是对于那些通过谷歌搜索出现在这个话题上的人来说,包括这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM