Reading System.in from the Console using Intellij and JUnit

后端 未结 2 1264
说谎
说谎 2021-01-23 10:24

The following code works nicely when running the code through a main in Idea

System.in.read()

However the same code inside a junit method is no

2条回答
  •  囚心锁ツ
    2021-01-23 11:00

    You need to use the Scanner class.

    First import it:

    import java.util.Scanner
    

    Now to use it:

    Scanner scanner = new Scanner(System.in);
    String text = scanner.next();
    scanner.close();
    

    There are more methods like scanner.nextLine(); that you should look at.

    Also you might need to handle some Exceptions here but that's not difficult to figure out.

提交回复
热议问题