布尔值到数组的字符串?
问题描述:
我创建了下面的代码,将字符串的所有值打印为'true'或'false'。我想填充一个数组,其中包含所有打印值“True true false true ....”现在,当我打印出String str的值时,如果它不在循环中,我只会得到第一个值字符。布尔值到数组的字符串?
// 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(phrase.charAt(i)));
System.out.println(str);
if (str == "true") {
isTrueCount++;
} else {
isFalseCount++;
}
}
System.out.println(isTrueCount);
System.out.println(isFalseCount);
}
len的长度是任意字符串/文本文件/键盘输入。我希望使用数组中的true和false值来从分隔符中挑出真正的单词数量。
答
不要为此使用字符串,因为它会增加不必要的复杂度。如果语句正确,只需增加变量:
for (int i = 0; i < phrase.length(); i++) {
if (!isDelim(phrase.charAt(i))) {
isTrue++;
} else {
isFalse++;
}
}
提供示例字符串。 –
请给我们len和短语的起始值。 – kroonwijk
您正在用循环的每次迭代重写字符串。也许你想要做'str + = String.valueOf(foo)+“”;'。更好的是,根本不要创建一个字符串,但将结果添加到'ArrayList'中。 –