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 trialEphraim Smith
11,930 PointsRecompile with -Xdiags:verbose to get full output
Created a workspace for practicing Java. I entered this simple while loop and I'm getting the error named in the question title. Not sure what's wrong..
int i=10; while (i>1) { console.printf(i); i--; }
Thanks in advance guys, -E
Ephraim Smith
11,930 PointsHey there again Yanuar,
Here's the link: https://w.trhou.se/8p28jqpz86. If you need something different, let me know.
Thanks, again!
1 Answer
Yanuar Prakoso
15,196 PointsHi ephraimsmith
The main problem of your code is: console.printf cannot directly print an int type variable in this case your i. You need to do one of this two things:
You need to convert your int variable i into string first using Integer.toString(i)
Since you are using printf you can use formatted syntax %s like console.printf("%s%n", i);
FYI I recommend the second approach since it will be used a lot through out Java courses. Please note I add %n after %s to give the printf a new line for each i. If you choose to use the first option you will print 1098765432treehouse:~/workspace$ in one line. the second option will look better.
This is your code should looks like:
import java.io.Console;
public class Introductions {
public static void main(String[] args) {
Console console = System.console();
//Worked along with "Feeling loopy with Java workshop
//**WHILE loops**
//Treehouse Example
/*String anyQuestions = console.readLine("Are there any questions? ");
while (anyQuestions.equals("yes")) {
String question = console.readLine("What is your question? ");
console.printf("I do not understand: %s %n", question);
anyQuestions = console.readLine("Are there any more questions?");
}
console.printf("Next slide...:");*/
//Web Example
int i=10;
while (i>1) {
//option 1:
console.printf(Integer.toString(i));
//option 2 (recommended):
console.printf("%s%n", i);
i--;
}
}
}
I hope this will help a little
Ephraim Smith
11,930 PointsThanks again Yanuar. Have a fun weekend.
Yanuar Prakoso
15,196 PointsYanuar Prakoso
15,196 PointsHi ephraimsmith...
Can you be kind and give me a snapshot of your workspace with this code in it. If You still have not known where the snapshot button in workspace is it is in the opposite side the menu bar, on the top right of your workspace window. It is located in one group with preview workspace and fork workspace buttons.