简体   繁体   English

Java字符串数组长度错误

[英]Java string array length error

I'm a beginner in Java and practicing to code by myself. 我是Java的初学者,并且自己练习编码。 I'm using a book to teach me how to code and I'm stuck on a project where it includes creating a String array and determining its length. 我正在使用一本书来教我如何编写代码,并且被困在一个项目中,该项目包括创建String数组并确定其长度。 Here's my sample work. 这是我的样本工作。 It's a simple baby name suggestion program. 这是一个简单的婴儿名字建议程序。

public class BabyName {
    public static void main(String[] args) {
        String[] nameListOne = {"Alvin", "Elmer", "Angel", "Michael", "Tim", "Jude", "Gabriel", "Raphael", "John", "Smith", "Carl", "Mike"};
        String[] nameListTwo = {"", "Mark", "Posh", "Jake", "Cloud", "Star", "Ben", "Sam", "Kim", "Mark", "Simeon", "Louie", "Nat", "Matt", ""};

        int nameOneLength = nameListOne.Length;
        int nameTwoLength = nameListTwo.Length;

        int rand1 = (int) (Math.random() * nameOneLength);
        int rand2 = (int) (Math.random() * nameTwoLength);

        String babyname = nameListOne[rand1] + " " + nameListTwo[rand2];

        System.out.println("Your suggested baby name is " + babyname);
    }
}

I then get this errors when I tried to compile it: 当我尝试编译它时,我得到以下错误:

BabyName.java:6: error: cannot find symbol
                int nameOneLength = nameListOne.Length;
                                               ^
  symbol:   variable Length
  location: variable nameListOne of type String[]
BabyName.java:7: error: cannot find symbol
                int nameTwoLength = nameListTwo.Length;
                                               ^
  symbol:   variable Length
  location: variable nameListTwo of type String[]
2 errors

I don't know what I did wrong here. 我不知道我在这里做错了什么。 I only followed what's written on the book. 我只看书上写的东西。

Hope someone can help. 希望有人能帮忙。

nameListOne.length长度应为小写

Java keywords are case sensitive. Java关键字区分大小写。 Use lowercase 'l' for 'length': nameListOne.length 将小写字母“ l”用作“长度”: nameListOne.length

It should be: 它应该是:

int nameOneLength = nameListOne.length;
int nameTwoLength = nameListTwo.length;

使用nameListOne.length Becoz Java区分大小写

Since you are a beginner, this would be helpful for you: Following are the naming conventions in Java ( a standard that should be (but not a must) followed) 由于您是初学者,因此这对您有所帮助:以下是Java中的命名约定(应该(但不是必须)遵循的标准)

  • All the variable names begin with a lower case eg length and are in CamelCasing 所有变量名称均以小写字母开头,例如长度,并在CamelCasing中
  • All Constants are all caps eg CONSTANT_LENGTH 所有常量都是大写字母,例如CONSTANT_LENGTH
  • All Class names begin with Capital letter and are in CamelCasing, eg LengthType 所有类名都以大写字母开头,并在CamelCasing中,例如LengthType

So, with this information in mind, you can detect such issues earlier (by following conventions) 因此,有了这些信息,您就可以更早地发现此类问题(遵循约定)

The issue with your code is "Length" is not a field, where as "length" is. 您的代码的问题是“长度”不是一个字段,而“长度”是一个字段。

Remember; 记得; Java is Case-Sensitive. Java是区分大小写的。

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

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