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

Madeline Yao
seal-mask
.a{fill-rule:evenodd;}techdegree
Madeline Yao
Full Stack JavaScript Techdegree Student 9,611 Points

Where is the bug

Hello everyone, I am trying to work on the challenge and everything looks alright but it turns out to have some problem and I do not know where is the problem? Could anyone help? Thanks.

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:");
    // 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
      ...
    */
    for(int i=0;i<programmers.size();i++){
      System.out.println((i+1)+programmers.get(i));

    }


  }

}

3 Answers

michaelcodes
michaelcodes
5,604 Points

Hi there! you where pretty close with what you have there, but for printing this output we must use a printf rather than println. The final for loop looking like this:

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

To get the programmers length we must use programmers.length rather than .size(). This is because it is a regular array and not an arraylist. It is easy to get confused between the two of them. For a regular array we use .length and to call the value [i]. For an arraylist we would use .size() and get(i)

We use the %d to represent the integer we need to print, then a period and space, then %s for the string, and a %n to put each output on a newline.

The values that will be displayed in the String are i+1 and the string programmers[i].

If you have any questions don't hesitate to ask. Take care and happy coding :)

Daniel Marin
Daniel Marin
8,021 Points

Hi Madeline Yao , michaelcodes .
So Madeline you were really close. It's good to see that the same problem can be solved a different way.

Ok so here's passing println:

    for(int i=0; i < programmers.length; i++){
      System.out.println(i + 1 + ". " + programmers[i] + "\n");
    }
Mark Casavantes
PLUS
Mark Casavantes
Courses Plus Student 13,401 Points

Good Morning Madeline and Michael,

I think \n needs to be used instead of %n for a new line in the print statement in Michael's code.