Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Getting Started with Java Receiving Input

How do we do the console.readLine but in an IDE?

Ive tried doing this in an IDE an it obviously did not work, but I do know for a fact that there is a way of doing it! if someone could answer this question that would be awesome!

How Do We Do The console.readLine in an IDE?

1 Answer

andren
andren
28,508 Points

The most common way of getting console input in an IDE is to use the Scanner class. Scanner has a method called nextLine which functions quite similarly to the readLine method.

The only notable difference is that readLine allows you to pass in a string which is then printed out at the same time as the console waits for input. That is not supported by nextLine so when using that you have to use a separate print command.

Here is example code that shows off how to use nextLine:

import java.util.Scanner; // Import Scanner

public class Example {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Initialize a Scanner object

        System.out.print("Please enter your name: ");
        String name = scanner.nextLine(); // Use the nextLine method to get input
        System.out.println("Your name is: " + name);
    }
}

Thank You!