Java 必须是数组类型但解析为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21110379/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:38:19  来源:igfitidea点击:

Must be an array type but resolved to string

javaarraysstring

提问by user2904796

I am getting the "Must be an array type but it resolved to string" error in my code. It also says that i (in the code below) cannot be resolved to a variable which I don't get.

我在我的代码中收到“必须是数组类型但它解析为字符串”错误。它还说 i (在下面的代码中)无法解析为我没有得到的变量。

    public class DNAcgcount{

        public double ratio(String dna){
        int count=0;
        for (int i=0;i<dna.length();i++);
            if (dna[i]== "c"){
            count+= 1;
            if (dna[i]=="g"){
            count+=1;
        double answer = count/dna.length();
        return answer;

    }

    }


}

}

}

Could you guys please help me figure out where the problem lies? I'm new to coding in Java so I am not entirely comfortable with the format yet.

请各位大侠帮我看看问题出在哪里?我是 Java 编码的新手,所以我对这种格式还不完全满意。

Thanks a lot, Junaid

非常感谢,朱奈德

回答by MByD

You cannot access a String's character using subscript (dna[i]). Use charAt instead:

您不能使用下标 ( dna[i])访问字符串的字符。使用 charAt 代替:

dna.charAt(i) == 'c'

Also, "c"is a String, 'c'is a char.

此外,"c"是一个String'c'是一个char

One more thing - integer division ( e.g. int_a / int_b) results in an int, and so you lose accuracy, instead - cast one of the ints to double:

另一件事 - 整数除法(例如int_a / int_b)导致一个 int,所以你失去了准确性,相反 - 将ints 之一转换为double

double answer = count/(double)dna.length();

回答by Damian Leszczyński - Vash

Your problem is with syntax dna[i], dnais a string and you access it as it would be an array by []. Use dna.charAt(i);instead.

你的问题是语法dna[i]dna是一个字符串,你访问它,因为它是一个数组[]。使用dna.charAt(i);来代替。

回答by giorashc

You using Stringincorrectly. Instead of accessing via []use dna.charAt(i).

你用String错了。而不是通过[]use访问dna.charAt(i)

Altough logically a string is an array of characters in Java a Stringtype is a class (which means it has attributes and methods) and not a typical array.

尽管从逻辑上讲,字符串是 Java 中的字符数组,但String类型是类(这意味着它具有属性和方法)而不是典型的数组。

And if you want to compare a single character to another enclose it with '' instead of "":

如果要将单个字符与另一个字符进行比较,请使用 '' 而不是 "" 将其括起来:

if (dna.charAt(i) == 'c')
.
.

回答by tobias_k

Use {}to define the scope of the loop. Also, as others already pointed out, use charAtinstead of []and use 'for characters, and use floating point division for the ratio.

使用{}来定义循环的范围。此外,正如其他人已经指出的那样,对字符使用charAt代替[]和使用',对ratio.

for (int i = 0; i < dna.length(); i++) {
    if (dna.charAt(i) == 'c') {
        count += 1;
    }
    if (dna.charAt(i) == 'g') {
        count += 1;
    }
}

Or a bit shorter, use ||to orthe two clauses together

或者更短一点,使用||to或者这两个子句一起

if (dna.charAt(i) == 'c' || dna.charAt(i) == 'g') {
    count += 1;
}

回答by Lukas Warsitz

It seems like that you have a few problems with the main syntax of basic java functions like loopsor if-elsestatement. Click herefor a good tutorial on these.

似乎您在基本 Java 函数(如loopsorif-else语句)的主要语法方面遇到了一些问题。单击此处获取有关这些的很好的教程。

You must correct your for-loopand your if-statement:

您必须更正您的for-loop和您的if-statement

for(int i=0;i<dna.length();i++){
    if(...){
       ...;
    }
    if(...){
       ...;
    }
}

Now you wont get the Cant be resolved to a variable... exception.

现在你不会得到Cant be resolved to a variable... exception.

Second thing is the usage of your string. You have to use it like this:

第二件事是你的字符串的使用。你必须像这样使用它:

for(int i=0;i<dna.length();i++){
    if(dna.charAt(i) == 'c'){
       count += 1;
    }
    if(dna.charAt(i) == 'g'){
       count += 1;
    }
}

Now all your exceptions should be eleminated.

现在应该消除所有例外情况。

回答by pshemek

There are two errors:

有两个错误:

count should be double or should be casted do double answer = (double)count / dna.length();

计数应该是双倍或应该被铸造 do double answer = (double)count / dna.length();

and as mentioned above you should replace dna[i] with dna.charAt(i)

如上所述,您应该将 dna[i] 替换为 dna.charAt(i)

回答by Darsh

I think you are currently a bit weak at brackets , this is what i understood from your code and corrected it;

我认为您目前在括号方面有点弱,这是我从您的代码中理解并更正的内容;

public class DNAcgcount{

    public double ratio(String dna){
    int count=0;
    for (int i=0;i<dna.length();i++){
        if (dna.charAt(i)== 'c')
        count+= 1;
        if (dna.charAt(i)=='g')
        count+=1;
    }
        double answer = count/(double)dna.length();
        return answer;
    }
}

After if we have to close the brackets when what you want in if is finished . I think you wanted countto be the number of time cor gis present in the dna. You also did some other mistakes like you have to use 'c'and 'g'instead of "c"and "g"if you are using .charAt(i)because it will be treated like a character and then only you can compare . You may view this link http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.htmlhttp://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

之后如果你想要的东西完成后我们必须关闭括号。我想你想要count的次数c或者g是出现在dna. 您还犯了一些其他错误,例如您必须使用'c'and'g'而不是"c"and"g"如果您正在使用,.charAt(i)因为它将被视为一个字符,然后只有您可以比较。您可以查看此链接 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

and you may also have a look at works you can do with string like charAt.

你也可以看看你可以用像charAt这样的字符串做的作品。