Java 检查字符串 x 是否等于来自 String[] 的任何字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18044469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if String x equals any of the Strings from String[]
提问by Matt Smith
I am trying to set a boolean true if a user-entered string equals any of the strings from a string array.
如果用户输入的字符串等于字符串数组中的任何字符串,我试图将布尔值设置为 true。
I have improvised and came up with this
我即兴创作并想出了这个
String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = true;
for(String cancelWord : cancelWords) {
if(!valueEqualsCancel) break;
valueEqualsCancel = valueEqualsCancel && value.equals(cancelWord);
}
But valueEqualsCancel
is never true.
但valueEqualsCancel
从来都不是真的。
Any tips?
有小费吗?
采纳答案by Fedy2
valueEqualsCancelis never true because you don't exit from the loop when you find the cancelWord.
valueEqualsCancel永远不会为真,因为当您找到cancelWord时您不会退出循环。
In order to reach the break statement you need valueEqualsCancel to be false.
为了达到 break 语句,您需要 valueEqualsCancel 为 false。
If you for example search for "cancel" after the first loop the variable valueEqualsCancel is:
例如,如果您在第一个循环后搜索“取消”,则变量 valueEqualsCancel 是:
valueEqualsCancel = valueEqualsCancel && value.equals(cancelWord) = TRUE && TRUE = TRUE;
so on the second loop you don't break. Then you evaluate the expression again
所以在第二个循环中你不会中断。然后你再次评估表达式
valueEqualsCancel = valueEqualsCancel && value.equals(cancelWord) = TRUE && FALSE = FALSE;
therefore on third loop you will exit and valueEqualsCancel is false.
因此在第三个循环中,您将退出并且 valueEqualsCancel 为 false。
You can correct your code in this way:
您可以通过以下方式更正您的代码:
String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean found = false;
for(String cancelWord : cancelWords) {
found = value.equals(cancelWord);
if (found) break;
}
回答by JHS
Convert the Array
to a List
and then use the contains
method to check.
将 转换Array
为 a List
,然后使用该contains
方法进行检查。
Arrays.asList(cancelWords).contains(value)
Arrays.asList(cancelWords).contains(value)
回答by Curt
foreach(string thisWord in cancelWords)
if thisWord.equals(value)
return true;
return false; // Fall-through
回答by Richard Sitze
Your initial value is off, and the flow can be improved:
您的初始值已关闭,可以改进流程:
String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = false; // fix
for(String cancelWord : cancelWords) {
if(value.equals(cancelWord)) {
valueEqualsCancel = true;
break;
}
}
回答by arshajii
You can do something like:
您可以执行以下操作:
Arrays.asList(cancelWords).contains(value)
(see Arrays.asList()
)
If you're going to be doing multiple queries like this, then put all of your words in a Set
and query that.
如果您要进行这样的多个查询,请将所有单词放在 a 中Set
并进行查询。
回答by Luke Taylor
Use this method:
使用这个方法:
public boolean containsWord(String word, String[] words) {
for(String cancelWord : words) {
if(word.equals(cancelWord)) {
return true;
}
}
return false;
}
I hope this helps.
我希望这有帮助。
回答by John Snow
I guess this should help:-
我想这应该会有所帮助:-
import java.util.Scanner;
public class check4 {
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
String value = scan.nextLine().toString();
String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = false;
for(int i=0;i<cancelWords.length;i++) {
if(value.equals(cancelWords[i])) {
valueEqualsCancel = true;
break;
}
}
System.out.println(valueEqualsCancel);
}
}
Input:
输入:
camel
Output:
输出:
true
回答by Anuswadh
try
尝试
String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = true;
for(String cancelWord : cancelWords) {
if(!valueEqualsCancel) break;
valueEqualsCancel = !(valueEqualsCancel && value.equals(cancelWord));
}
回答by Obzer_G
Use this Method
使用此方法
public static boolean isContainsWords(String[] words,String Targent){
boolean flag;
for(String buf : words){
if(buf.equals(Targent))
return flag = true;
}
return flag = false;
}
It seems perfect
看起来很完美
回答by jboi
The solution that fits best to your situation depends a bit on the number of cancel words and the necessity to compare case insensitive or not. I'Ve added code for two possbile ways to implement:
最适合您情况的解决方案取决于取消字的数量以及比较不区分大小写的必要性。我为两种可能的实现方式添加了代码:
// I would use a HashSet which needs constant time to compare, even with long lists of cancel words
// Note, that the use of toLowerCase() makes the comparison case insensitive.
@Test
public final void test2() {
String value = "nevermind";
HashSet<String> cancelWords = new HashSet<String>();
cancelWords.addAll(Arrays.asList(new String[] {"cancel", "nevermind", "scratch that"}));
boolean valueEqualsCancel = cancelWords.contains(value.toLowerCase());
System.out.println("test2: " + valueEqualsCancel);
}
// You might like to know, which cancel word it was
@Test
public final void test3() {
String value = "nevermind";
String[] cancelWords = {"cancel", "nevermind", "scratch that"};
Arrays.sort(cancelWords); // Prepare for binary search
// The returned value indicates either which cancel word was used or, that nothing was found with result < 0.
System.out.println("test3: nevermind " + Arrays.binarySearch(cancelWords, "nevermind"));
System.out.println("test3: cancel " + Arrays.binarySearch(cancelWords, "cancel"));
System.out.println("test3: something totally different " + Arrays.binarySearch(cancelWords, "something totally different"));
}