英文:
How to check the lenght of an array of strings? I don't want to use lenght() because I need to know how many strings are in the array
问题
public class Programma {
public static void main(String args[]) {
System.out.println("The number of words equal to Ciao is: " + countWord(args));
}
static int countWord(String array[]) {
int counter = 0;
for (int i = 0; i < array.length; ++i) {
if (array[i].equals("Ciao"))
++counter;
}
return counter;
}
}
Output:
Programma.java:9: error: cannot find symbol
for (int i=0; i<array.length; ++i){
^
symbol: variable length
location: variable array of type String[]
1 error
Why? I don't want to use `length()` because I don't want to know the *length* of the string, but I actually want to know how many strings are in the array.
英文:
public class Programma{
public static void main(String args[]){
System.out.println("Il numero di parole uguali a Ciao è: " + contaparola(args));
}
static int contaparola(String array[]){
int counter = 0;
for (int i=0; i<array.lenght; ++i){
if (array[i].equals("Ciao"))
++counter;
}
return counter;
}
}
Output:
Programma.java:9: error: cannot find symbol
for (int i=0; i<array.lenght; ++i){
^
symbol: variable lenght
location: variable array of type String[]
1 error
Why? I dont want to use lenght()
because I don't want to know the lenght of the string, but I actually want to know how many strings are in the array.
答案1
得分: 3
.length
不是字符串的长度。它是数组的长度,或者说是“可以存储在数组中的项目数。
您的程序主要问题是您拼写错误了length
。
英文:
.length
isn't the length of a string. It's the length of an array, or "the number of items that could be stored in the array.
The main problem in your program is that you misspelled length
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论