简体   繁体   English

使用arrayName [i] [j] .equalsIgnoreCase在2D数组中进行Java搜索找不到值

[英]Java Search In 2D Array using arrayName[i][j].equalsIgnoreCase not finding value

I've been searching the community for the past few hours looking for a solution. 在过去的几个小时中,我一直在社区中寻找解决方案。 If you find this as a duplicate please close and direct me to the solution. 如果您发现此重复项,请关闭并将我定向到解决方案。 Thanks in advance! 提前致谢!

I'm searching through a 2D array looking to match a card combination entered in a TextView with a card combination in the array. 我正在搜索2D数组,以使在TextView中输入的卡片组合与数组中的卡片组合相匹配。 Code is not recognizing that it has found the correct entry and always loops through as False. 代码无法识别已找到正确的条目,并且始终循环为False。 I'm wondering if I'm not storing and searching for the string in the correct way. 我想知道我是否没有以正确的方式存储和搜索字符串。

For example, if in the TextView I entered Q3 the Logcat will show: 例如,如果在TextView中输入了Q3,则Logcat将显示:

Checking:: [Q3] Searching:: [Q3] 检查:: [Q3]搜索:: [Q3]

So I know we're looping through and hitting the correct item in the Array. 所以我知道我们正在遍历并击中Array中的正确项目。 But I cannot get the code to recognize it. 但是我无法获得识别它的代码。

//Get the text from the holeCards TextView
holeCards = (TextView)findViewById(R.id.enter_cards_text);
String holeCardsText = ((TextView) holeCards).getText().toString();

//Search cardParisArray for holeCardsText

int rows = cardPairsArray.length;
int columns = cardPairsArray[0].length;
String cardPairArrayRows = Integer.toString(rows);

for (int i = 0; i < rows; i++) {

    for (int j = 0; j < columns; j++) {

        Log.d("Searching:", Arrays.toString(new String[]{holeCardsText}));
        Log.d("Checking:", Arrays.toString(new String[]{cardPairsArray[i][j]}));

        if (cardPairsArray[i][j].equalsIgnoreCase(holeCardsText) == true) {

            responseTextView.setText("Cards were found");

        }else responseTextView.setText("Cards not found");
    }
}

Solution was twofold. 解决方案是双重的。 Else statement was overriding when the string was found. 找到字符串时,其他语句将被覆盖。 So I removed the else statement. 所以我删除了else语句。

Second contains requires case to match so I ensured that text entered in the Text View is converted to capital. 第二个包含要求大小写匹配的字母,因此我确保在“文本视图”中输入的文本将转换为大写。

Please feel free to comment if you have a better answer. 如果您有更好的答案,请随时发表评论。

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

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