简体   繁体   中英

How can I read input from console till a character is typed

I want to read input from a console for example
Enter input:
aabbbab
execute the method when pressed Enter and keep asking for input and execute till a character is typed in. At the moment I can read an input and enter it execute it.Then I need to run a program again to enter a new input.

BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
while(true){
    String text = console.readLine();
    doStuff(text)
}

We can read string from console application using console class

 String username = console.readLine("Enter username: ");
 System.out.println(username);

I think what you are asking for is to do something (in this case taking input and working on it) repeatedly. It should be clear that when you want to do the same thing repeatedly, you should put that code inside a loop. What that loop looks like (eg for, do, while etc.) and what its exit condition is, is up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM