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 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,558 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!