0

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.

3
  • 3
    You want length, not Length. Commented Jul 6, 2015 at 7:44
  • Upvote for @JonSkeet who technically answered it first! Commented Jul 6, 2015 at 7:45
  • 1
    ... but voted to close as a typo, basically. Commented Jul 6, 2015 at 7:46

5 Answers 5

2

nameListOne.length length should be lowercase

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

Yes. Really need to get used with these case sensitive thing. Thanks.
1

It should be:

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

Comments

0

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.

1 Comment

Thank you for the additional info. Will take note of that. Thanks again.
0

Use nameListOne.length Becoz Java is case-Sensetive

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.