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

An alternative to console.readline in IDE

I am using an IDE instead of the console because before coming to team treehouse I always used IntelliJ and was used to it. When I checked out the workspace console it uses the console object to run code and everywhere else I have been I have never seen it used even in code samples I checked online. So I decided to continue with IntelliJ since I know the alternatives to some of the console methods. Now I am stuck on the video where they are talking about receiving input and in the video the instructor uses console.readline. Please does anyone know what I can use in place of this if I am not using the console object?

1 Answer

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Hello Ibrahim Adeosun

I also had the same dilemma you have because IDE is where the real power and fun is.

Luckily the console object can be replaced with the Scanner Object instead of console. You will only need to import the Scanner Object using this line ** import java.util.Scanner; ** Then you need to use Scanner's method nextLine() to read input.

Take a look at this example:

// Console console = System.console(); remove the Console Object and replace it with Scanner
   Scanner scanner = new Scanner(System.in);
   System.out.println("Enter your name");
    String name = scanner. nextLine(); 
System.out.println("Enter an adjective");
    String adjective = scanner. nextLine(); 
   System.out.printf("%s is very %s", name, adjective);

The only disadvantage of the Scanner Object is you have to prompt the user manually using *System.out.println() * method.

Please let me know if it works for you.

Cheers.

Thank you ver much. I will try it out.