简体   繁体   English

如何使用Java方法返回的布尔值?

[英]How do I use the Boolean values returned by a Java method?

I have a method that sends a bunch of characters to another method that will return true or false if certain character are present. 我有一个将一堆字符发送到另一个方法的方法,如果存在某些字符,该方法将返回true或false。 Once this method evaluates all the character and returns true or false for each of them how do I use those true or false vales in another method? 一旦此方法评估了所有字符并为每个字符返回true或false,我如何在另一种方法中使用这些true或false值? I think I have the method sending one character at a time. 我想我可以一次发送一个字符。 The Boolean method is not a boolean array. 布尔方法不是布尔数组。

    public void Parse(String phrase)
{
    // First find out how many words and store in numWords
    // Use "isDelim()" to determine delimiters versus words
    //
    // Create wordArr big enough to hold numWords Strings
    // Fill up wordArr with words found in "phrase" parameter


    int len = phrase.length();
    char[] SeperateString = new char[len];
    for (int i = 0; i < len; i++) {
        SeperateString[i] = phrase.charAt(i);
        isDelim(SeperateString[i]);
        System.out.println(SeperateString[i]);

  boolean isDelim(char c)
{


    {
        if (c == delims[0])
            return true;
        else if (c == delims[1])
            return true;

        else if (c == delims[2])
            return true;
        else
            return false;

    }

    //Return true if c is one of the characters in the delims array, otherwise return false

}

so your method is like this 所以你的方法是这样的

boolean myMethod(String s) {
    // returns true or false somehow... doesn't matter how
}

you can do this later 你可以稍后再做

boolean b = myMethod("some string");

someOtherMethod(b);

or even 甚至

someOtherMethod(myMethod("some string"));

Now if your method is returning lots of booleans, say one for each character, it would look more like this 现在,如果您的方法返回很多布尔值,请为每个字符说一个布尔值,它将看起来像这样

boolean[] myMethod(String s) {
    // generates the booleans
}

You'd have to access them some other manner, perhaps like so: 您可能必须以其他方式访问它们,例如:

String str = "some string";
boolean[] bools = myMethod(str);
for(int i = 0; i < str.length(); i++) {
    someOtherMethod(bools[i]);
}

For more information on this you'd have to post your code. 有关此的更多信息,您必须发布代码。

In response to the posted code, here's what I would do 响应发布的代码,这就是我要做的

/**
 * Parse a string to get how many words it has
 * @param s the string to parse
 * @return the number of words in the string
 */
public int parse(String s) {
    int numWords = 1; // always at least one word, right?
    for(int i = 0; i < s.length(); i++) {
        if(isDelim(s.charAt(i)) numWords++;
    }
    return numWords;
}

private boolean isDelim(char c) {
    for(char d : delims) if(c == d) return true;
    return false;
}

From what I see, you could be using String.split() instead. 据我所知,您可能正在使用String.split()。 You can use regex with it as well, so you can include your 3 different delimiters. 您也可以将regex与其一起使用,因此可以包括3个不同的定界符。 If you need the number of works still you can get the result's length. 如果您仍然需要大量作品,则可以得到结果的长度。

http://download.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29 http://download.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

Assuming delims is a character array, it would be safe to use this to generate the regex: 假设delims是一个字符数组,使用它生成正则表达式将是安全的:

String regex = "[" + new String(delims) + "]";
String result = params.split(regex);

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

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