Reputation: 13
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
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)
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
Reputation: 56
It should be:
int nameOneLength = nameListOne.length;
int nameTwoLength = nameListTwo.length;
Upvotes: 1
Reputation: 31269
Java keywords are case sensitive. Use lowercase 'l' for 'length': nameListOne.length
Upvotes: 2