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 trialarnaud
13,859 Points"System.out.printf" or "console.printf" ?
I'm using an IDE to practice, which is working just fine with other languages. However, I sometimes find it difficult to complete exercises with Java. Should I use "System.out.printf" or "console.printf" ? It seems that both work occasionally, but not always, which is a bit annoying. The set up below is currently working, but I don't want to have to adjust every new challenge.
import javax.accessibility.*;
import java.io.*;
import java.io.Console;
import javax.lang.model.element.*;
class Untitled {
public static void main(String[] args) {
Console console = System.console();
String name = console.readLine("Enter your name: ");
String adjective = console.readLine("Enter an adjective: ");
console.printf("%s is a very %s man", name, adjective);
}
}
Cheers.
4 Answers
Caleb Kleveter
Treehouse Moderator 37,862 PointsWhat if you use:
System.out.println
Does it work?
Geoffrey de Vlugt
4,014 PointsHello arnaud,
Perhaps you have already found an answer to your question, but perhaps the thread below might explain why it's not always working:
https://teamtreehouse.com/community/what-is-the-difference-between-systemoutprint-and-consoleprintf
David Smith
10,577 PointsI know this is a little old but I too had this problem and found the answer here: https://stackoverflow.com/questions/26470972/trying-to-read-from-the-console-in-java
arnaud
13,859 PointsThanks Caleb,
"System.out.println" is working, and so are "System.out.printf" or "console.printf", but they don't work all the time, so I have to switch from one to another sometimes, which is not ideal. What is the difference between all three, and is there one that is always guaranteed to work in all IDEs?
Caleb Kleveter
Treehouse Moderator 37,862 PointsCaleb Kleveter
Treehouse Moderator 37,862 PointsHi Arnaud, I formatted you code so it is easier for people to read. Hope you get an answer!