简体   繁体   中英

Take input from the user using java

I am creating a console using this method

MessageConsole console = new MessageConsole("", null);
console.activate();
ConsolePlugin.getDefault().getConsoleManager()
        .addConsoles(new IConsole[] { console });
MessageConsoleStream stream = console.newMessageStream();
System.setOut(new PrintStream(stream, true));

I tried the normal method I use for taking an input but it's not working Here is the code I tried

[1] This prints 'Enter a string' but doesn't allow you to write in the console

Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
String s = in.nextLine();
System.out.println("You entered string " + s);

[2] and this prints null

BufferedReader columnInput = new BufferedReader(new InputStreamReader(
        System.in));
try {
    System.out.println(columnInput.readLine());
    // System.out.println("#read mflkerf: " );
} catch (IOException e) {

}

Your first code snippet:

Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
String s = in.nextLine();
System.out.println("You entered string " + s);

does work. I'm guessing because you reassigned standard output stream using System.setOut you no longer see the output printed from System.out.println

Try creating a thread that prints your new console outputstream into a file or somewhere and you will see the output

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