简体   繁体   English

如何使用Java代码在Eclipse控制台中插入值

[英]How to insert value in console of eclipse using java code

I want to enter value in console at run-time (during execution), rather than hard code it manually. 我想在运行时(执行期间)在控制台中输入值,而不是手动对其进行硬编码。 I have following code: 我有以下代码:

System.out.println("Open the following URL and grant access to your account:");
System.out.println(requestToken.getAuthorizationURL());
String authURL=requestToken.getAuthorizationURL();
//Generate a pin number from auth url by login to twitter account.
String pinNum=GenratePIN_LoginTwitter.accessTokenForTwitter(authURL);
System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
String pin =br.readLine();   

Here I want to insert the value of 'pinNum' in console and proceed with the next steps.Please suggest regarding it. 在这里,我想在控制台中插入'pinNum'的值并继续下一步,请提出建议。

If you want to get the input from the user from console use java.util.Scanner class 如果要从控制台获取用户输入,请使用java.util.Scanner

 System.out.println("Enter a number: ")
 Scanner sc = new Scanner(System.in);
 int i = sc.nextInt();
 System.out.println("The number is: "+i);

The above code will help you to get the input from console 上面的代码将帮助您从控制台获取输入

For more on Scanner refer Oracle docs 有关Scanner更多信息,请参考Oracle文档

Basically System.in is The standard input stream .this stream corresponds to keyboard input or another input source specified by the host environment or user. 基本上System.in标准输入流。此流对应于键盘输入或主机环境或用户指定的另一个输入源。 so you can use it in some of the java IO API.as @abhishek told for Scanner you can also use below code. 因此您可以在某些Java IO API中使用它。正如@abhishek告诉Scanner的那样,您也可以使用以下代码。

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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