开发者

String of boolean values to an array?

开发者 https://www.devze.com 2023-04-10 04:51 出处:网络
I created the code below to print out all of the values of a string to \'true\' or \'false\'.I would like to fill an array with all the printed values \"True true false true....\"Right now when I prin

I created the code below to print out all of the values of a string to 'true' or 'false'. I would like to fill an array with all the printed values "True true false true...." Right now when I print out the values of the String str if it is not in the loop I only get the first value of the first character.

    // 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
    public void Parse(String phrase) {
      int len = phrase.length(); 
      String str = null;

      int isTrueCount = 0;
      int isFalseCount = 0;

      for (int i = 0; i < (len); i++) { 
        str = String.valueOf(!isDelim(phras开发者_C百科e.charAt(i)));
        System.out.println(str);

        if (str == "true") {
          isTrueCount++;
        } else {
          isFalseCount++;
        }
      }
      System.out.println(isTrueCount);
      System.out.println(isFalseCount);
    }

Length of len is any arbitrary string/text file/keyboard input. I am hoping to use the true and false values within an array to pick out the true number of words from delims.


Don't use Strings for this as it adds unnecessary complexity. Simply increment your variable if the statement is true:

  for (int i = 0; i < phrase.length(); i++) {
     if (!isDelim(phrase.charAt(i))) {
        isTrue++;
     } else {
        isFalse++;
     }
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消