Alvin
Alvin

Reputation: 13

Java string array length error

I'm a beginner in Java and practicing to code by myself. 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. 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.

Upvotes: 0

Views: 311

Answers (5)

Mohit Kanwar
Mohit Kanwar

Reputation: 3050

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)

  • All the variable names begin with a lower case e.g. length and are in CamelCasing
  • All Constants are all caps e.g. CONSTANT_LENGTH
  • All Class names begin with Capital letter and are in CamelCasing, e.g. 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.

Upvotes: 0

Amit
Amit

Reputation: 43

Use nameListOne.length Becoz Java is case-Sensetive

Upvotes: 0

Igor Lopes
Igor Lopes

Reputation: 56

It should be:

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

Upvotes: 1

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31269

Java keywords are case sensitive. Use lowercase 'l' for 'length': nameListOne.length

Upvotes: 2

codeaholicguy
codeaholicguy

Reputation: 1691

nameListOne.length length should be lowercase

Upvotes: 2

Related Questions