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 Getting Started with Java IO

Sanjay Nair
PLUS
Sanjay Nair
Courses Plus Student 2,477 Points

console.readLine in not working with eclipse, Could you have on this?

When I try to run the example of eclipse it's not working. but it works on terminal. Can you help how can I run the program on eclipse as well?

2 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

In Eclipse or other IDE you cannot use Console.readLine, for detailed explanation why, see here:

http://stackoverflow.com/questions/26470972/trying-to-read-from-console-java

In order to prompt user for input in IDE commonly BufferedReader or Scanner are used. For what is better see here:

http://stackoverflow.com/questions/2231369/scanner-vs-bufferedreader

I prefer BufferedReader, well I didn't dig up well Scanner and I know how to mock BufferedReader to test user input in Unit Testing. Here is code example how BufferedReader can be used:

public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader =  new BufferedReader(new InputStreamReader(System.in));
        String userInputString = "";
        try {
            userInputString = mBufferedReader.readLine();
        } catch (IOException ioe) {
            throw new IOException("Problems with reading user input");
        }
        // here you can use "userInputString"
}