Problem with BufferedReader.readLine using sbt run or sbt console

My question is quick. I work on a small console to read input, and then calling the appropriate code. I use sbt, and I ran into a problem when trying to read input after starting my program with sbt running, inside the sbt console or even in the regular old scala interpreter.

It seems that the prompt just hangs, but if I press return, it really reads the input. Although the shell buffer remains empty. Here is the generic code I tried that gave me this problem.

import java.io._ val s = new BufferedReader(new InputStreamReader(System.in)) val line = s.readLine println(line) 

Does anyone know why this is, and if there is a way to fix it? I would like to see what I type when I run my program from sbt. Not seeing how I type in the shell, it makes testing and developing my project much less enjoyable.

+6
source share
1 answer

This is really a Java API issue, though in Scala. BufferedReader.readLine () will use all the characters you enter from System.in until there is a whole line, after which it will return the line as you said.

Entering the console was difficult in Java with the java.io source classes. Before Java6, I saw a couple of useless solutions, but luckily a new class was introduced with this version to make it a lot easier: java.io.Console . I think it becomes as simple as

 val line = System.console.readLine println(line) 
-1
source

Source: https://habr.com/ru/post/910560/


All Articles