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

Java Java Basics Using your New Tools Multiple Strings

"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.

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Hi Arnaud, I formatted you code so it is easier for people to read. Hope you get an answer!

4 Answers

Geoffrey de Vlugt
Geoffrey de Vlugt
4,014 Points

Hello 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
David Smith
10,577 Points

I 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

Thanks 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?