简体   繁体   English

将字符与Java中的字符串数组中的元素进行比较

[英]Compare character to the elements inside a array of strings in Java

public static int getDigit(char character, char letters[]  )
{
    int digit=0;
    for (int i=0;i<=0;i++);
    {
        for (int j=0; j<(letters[i].length());j++)
        {
            if (letters[i][j]==character)
            {
               digit=i; 
            }
        }    
    }
    return digit;
}

where array is LETTERS={("abc"),("def"),("ghi"),("jkl"), ("mno)"),("pqrs"),("tuv"),("wxyz")}; 其中array为LETTERS = {(“ abc”),(“ def”),(“ ghi”),(“ jkl”),(“ mno)”),(“ pqrs”),(“ tuv”),( “ wxyz”)}; and character could be any alphabet. 字符可以是任何字母。

I want program to find a character entered and to display the location of that in the string. 我希望程序查找输入的字符并在字符串中显示该字符的位置。 Eg- If user enters letter "g" then it should display number 3 because it is in 3rd element of array. 例如,如果用户输入字母“ g”,则它应显示数字3,因为它在数组的第3个元素中。

Here is a working implementation of what you appear to want. 这是您想要的工作实例。 I assume that your letters array is intended to be a 2D jagged character array. 我假设您的letters数组旨在成为2D锯齿字符数组。 Your original code really only had a few minor issues, mainly in the loop logic. 您的原始代码实际上只有几个小问题,主要是在循环逻辑中。

public static int getDigit(char character, char letters[][]) {
    int digit = -1; // return -1 if character not found

    for (int i=0;i < letters.length; i++) {
        for (int j=0; j < letters[i].length; j++) {
            if (letters[i][j] == character) {
                digit = i;
                i = letters.length;
                break;
            }
        }    
    }

    return digit;
}

public static void main(String args[]) {
    char[][] letters = new char[][] {
        { 'a', 'b', 'c' },
        { 'd', 'e', 'f' },
        { 'g', 'h', 'i' },
        { 'j', 'k', 'l' },
        { 'm', 'n', 'o' },
        { 'p', 'q', 'r', 's' },
        { 't', 'u', 'v' },
        { 'w', 'x', 'y', 'z' }};
    System.out.println(getDigit('d', letters) );
}

Demo 演示版

It's not really possible to completely answer you question because it doesn't say what the problem is besides "it doesn't work". 不可能完全回答您的问题,因为除了“它不起作用”之外,它没有说出问题所在。

Just skimming the code: you should probably change the line digit=i; 只需浏览一下代码:您可能应该更改digit=i; to return i + 1; return i + 1; because you said it should return 3 if "g" is the third character. 因为您说如果第三个字符“ g”应该返回3。

Also, this loop is nonsense for (int i=0;i<=0;i++); 同样, for (int i=0;i<=0;i++);此循环是无意义for (int i=0;i<=0;i++); It will run only once ever... Also as @bcsb1001 pointed out in the comments, the semicolon at the end of the loop essentially ends it, so the loop is actually empty! 它将仅运行一次...也正如@ bcsb1001在注释中指出的那样,循环末尾的分号实质上结束了它,因此循环实际上是空的! It should be for (int i=0;i<letters.length();i++) { 应该是for (int i=0;i<letters.length();i++) {

There might be other issues with this code. 此代码可能还有其他问题。 You didn't share enough. 您分享的次数不够。

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

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