在 eclipse 中获取 STDIN?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19381779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:15:26  来源:igfitidea点击:

Getting STDIN in eclipse?

eclipsestdin

提问by galdikas

I need to write a code for competition using STDIN and STDOUT, how can I set up eclipse to take input from STDIN??

我需要使用 STDIN 和 STDOUT 编写比赛代码,如何设置 eclipse 以接收来自 STDIN 的输入?

When I was just using editor and command line I was running:

当我只使用编辑器和命令行时,我正在运行:

java Hatch < hatch_input

Now I need the equivalent to work directly from eclipse. It would be perfect if I could use it directly from file, but it would to suffice to just be able to paste the actual input somewhere, as long as I can write code that takes STDIN directly in eclipse by clicking RUN.

现在我需要等价物直接从 Eclipse 工作。如果我可以直接从文件中使用它就完美了,但是只要我可以通过单击 RUN 来编写直接在 Eclipse 中使用 STDIN 的代码,只要能够将实际输入粘贴到某处就足够了。

class Main{

       public static void main (String[] args) throws java.lang.Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    }

}

Above code is what I am trying to achieve.

上面的代码是我想要实现的。

采纳答案by Fredrik

When you run your program you can setup a run configuration. Just click on on the arrow next to the play button and choose "Run configurations..."

运行程序时,您可以设置运行配置。只需单击播放按钮旁边的箭头并选择“运行配置...”

In the new run configuration dialog you create a new Java application run configuration. In the tab "Arguments" you can enter the arguments you want the program to receive.

在新的运行配置对话框中,您可以创建一个新的 Java 应用程序运行配置。在“参数”选项卡中,您可以输入希望程序接收的参数。

Suggestion: In the last tab, "common", choose to save it as a shared file, this will create a *.launch file that you can use to quickly run the program. You can also create different launch files for different inputs.

建议:在最后一个选项卡“common”中,选择将其保存为共享文件,这将创建一个*.launch文件,您可以使用它来快速运行程序。您还可以为不同的输入创建不同的启动文件。

回答by ChathuraG

You can also just type in the input data or copy paste in the console window of the eclipse. for Sdtin when the program hits that point focus shifts to console and waits for the response.

您也可以在 eclipse 的控制台窗口中输入输入数据或复制粘贴。对于 Sdtin,当程序到达该点时,焦点会转移到控制台并等待响应。

回答by 13hola

There is following code,which i tried. this code is working fine if you run program in command prompt, but in eclipse not working because there console are no longer waiting for user input and give the java.lang.NullPointerException.

有以下代码,我试过。如果您在命令提示符下运行程序,此代码工作正常,但在 eclipse 中不起作用,因为控制台不再等待用户输入并给出java.lang.NullPointerException.

if you are read data by user input you can use System.in.read(). and this is also working in eclipse console.

如果您通过用户输入读取数据,则可以使用System.in.read(). 这也适用于 eclipse 控制台。

In this code you can get Data from user but this data is not argswhich we throw at run time...

在这段代码中,您可以从用户那里获取数据,但这些数据不是args我们在运行时抛出的...

public class CONSOLE
{
    public static void main(String[] args)
    {   
        // TODO Auto-generated method stub
        Console c = System.console();

        System.out.println("Enter Username : \t");
        String u = c.readLine();

        System.out.println("Enter Password : \t");
        char[] p = c.readPassword();

        System.out.println("USERNAME : "+u);
        System.out.println("PASSWORD : "+String.valueOf(p));
    }
}

回答by M. Dhaouadi

Scanner sc=new Scanner(System.in);
String s=sc.nextLine();