Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Arrays Iteration Looping Like These Programmers

Damjan Vlaic
Damjan Vlaic
19,244 Points

---JAVA---

i don't know why i am getting error in this for loop here, can someone pls help

In the printMenu method of the Programmers.java class, I'd like to print a menu showing off some famous programmers. Follow the TODOs in the code.

Programmers.java
public class Programmers {

  public void printMenu() {
    String[] programmers = {
            "Yukihiro Matsumoto",
            "David Nolen",
            "Grace Hopper",
            "Linus Torvalds",
            "You"
    };

    System.out.println("Choose a programmer:");
    for (int i = 0; i < programmers.lenght; i++) {
      System.out.printf("%d. %s", i + 1, programmers[i]);
    }
    // TODO: Print out a menu by looping through the programmers array.
    /*
      The menu should be in the form of (each on a line of its own, starting with 1):
      1. Yukihiro Matsumoto
      2. David Nolen
      ...
    */
  }
}

2 Answers

andren
andren
28,524 Points

When you get an error it can often help to take a look at the compiler warning that your code generates. That can be done by clicking on the "Preview" button on the top of the code editor. The compiler error generated by your code is this:

./Programmers.java:13: error: cannot find symbol
    for (int i = 0; i < programmers.lenght; i++) {
                                   ^
  symbol:   variable lenght
  location: variable programmers of type String[]
1 error

Java complains that it cannot find something called "lenght", that is why your code does not compile. The reason why it cannot find it is that the word has a typo. You have switched the t and h around in the word length.

If you fix that typo then your code will compile, though your code will still not pass due to you not having any line breaks in your print statement. If you add one like this:

public class Programmers {

  public void printMenu() {
    String[] programmers = {
            "Yukihiro Matsumoto",
            "David Nolen",
            "Grace Hopper",
            "Linus Torvalds",
            "You"
    };

    System.out.println("Choose a programmer:");
    for (int i = 0; i < programmers.length; i++) {
      System.out.printf("%d. %s%n", i + 1, programmers[i]);
    }
  }
}

Then your code will pass.

Damjan Vlaic
Damjan Vlaic
19,244 Points

You helped a lot, i guess i should work on my english a little hahaah ;)

andren
andren
28,524 Points

Your English seems completely fine to me :smile:. That particular typo is pretty easy to do, I've done it multiple times myself. And small typos like that are inevitable when you get into programming, that's why looking at compiler warnings is so important.

It often helps you narrow your focus down to the section of code which contains the typo, which becomes important when you start writing programs that can easily contain hundreds of lines of code per file.