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

huangapple go评论76阅读模式
英文:

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(&quot;Il numero di parole uguali a Ciao &#232;: &quot; + contaparola(args));
        }

    static int contaparola(String array[]){
        int counter = 0;
        for (int i=0; i&lt;array.lenght; ++i){
            if (array[i].equals(&quot;Ciao&quot;))
                ++counter;
        }
        return counter;
    }
}

Output:

Programma.java:9: error: cannot find symbol
                for (int i=0; i&lt;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.

huangapple
  • 本文由 发表于 2020年10月5日 00:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64197158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定