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 trial
Jerime McCastle
1,274 Pointsconsole question
import java.io.Console;
public class TreeStory {
public static void main (String [] args) {
Console console = System.console();
int age = 29;
String old = console.readLine("How old are you? ");
if (age < 19)
{
System.out.println("You are not an adult");
}
else
{
console.printf ("You are an adult\n");
}
}
}
System.out.println and console.printf can be used interchangeably. I would like to know when these should be used.
1 Answer
William Li
Courses Plus Student 26,868 PointsSpeaking from my experience, yeah, System.out.println and console.printf can be used interchangeably as long as you're in a console environment.
But when you're using an IDE, Eclipse for example, System.console() can give you a NullPointerException because you aren't running the Java code in a console.