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 Objects Meet Objects Welcome Back

harun west
harun west
1,732 Points

IntelliJ IDEA gives error when I use console.printf or console.readLine. But it works in Treehouse...?

Basically if I paste the code I've learnt in Treehouse to IntelliJ IDEA it doesn't work. Why is this? Am I learning the same Java or a different type?

1 Answer

andren
andren
28,558 Points

The problem is that the Console class does not work in most Java IDEs due to the way they run your program. This stackoverflow post provides a more detailed explanation as to why it does not work if you are curious. If you compiled your program in IntelliJ and ran it in a console manually then it would work fine.

This means that you have to use alternative ways to print and get input when working in your IDE.

To print you can use System.out instead of console like this:

System.out.println("Text you want to be printed to console");

System.out also has a printf and print method just like the Console class, that works in the same way as the Console equivalents

And to get input you can use this code:

Scanner scanner = new Scanner(System.in); // Write this in your code once
String input = scanner.nextLine(); // Then use scanner.nextLine as a substitute for console.readLine
harun west
harun west
1,732 Points

Brilliant. Thank you