Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMadeline Yao
Full Stack JavaScript Techdegree Student 9,611 PointsWhere 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.
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
5,604 PointsHi 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
8,021 PointsHi 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
Courses Plus Student 13,401 PointsGood 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.
Madeline Yao
Full Stack JavaScript Techdegree Student 9,611 PointsMadeline Yao
Full Stack JavaScript Techdegree Student 9,611 PointsThank you Michael. :)