我想使用Console类来从用户获取输入,但是当我呼叫System.console()
时返回空对象。在使用System.console之前,我必须更改任何内容吗?Java:如何从System.console()获取输入
Console co=System.console();
System.out.println(co);
try{
String s=co.readLine();
}
我想使用Console类来从用户获取输入,但是当我呼叫System.console()
时返回空对象。在使用System.console之前,我必须更改任何内容吗?Java:如何从System.console()获取输入
Console co=System.console();
System.out.println(co);
try{
String s=co.readLine();
}
这将取决于您的环境。例如,如果您通过javaw
运行Swing UI,那么不是要显示的控制台。如果您在IDE中运行,它将非常依赖于特定IDE对控制台IO的处理。
从命令行,它应该没问题。示例:
import java.io.Console;
public class Test {
public static void main(String[] args) throws Exception {
Console console = System.console();
if (console == null) {
System.out.println("Unable to fetch console");
return;
}
String line = console.readLine();
console.printf("I saw this line: %s", line);
}
}
运行这只是java
:
> javac Test.java
> java Test
Foo <---- entered by the user
I saw this line: Foo <---- program output
另一种选择是使用System.in
,你可能希望在BufferedReader
换到读线,或使用Scanner
(再次包装System.in
) 。
Scanner in = new Scanner(System.in);
int i = in.nextInt();
String s = in.next();
使用控制台读取输入(仅可使用IDE外):
System.out.print("Enter something:");
String input = System.console().readLine();
另一种方式(作品无处不在):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter String");
String s = br.readLine();
System.out.print("Enter Integer:");
try{
int i = Integer.parseInt(br.readLine());
}catch(NumberFormatException nfe){
System.err.println("Invalid Format!");
}
}
}
System.console()在返回null IDE
因此,如果您确实需要使用System.console(),请阅读此solution from McDowell。
发现了一些很好的答案就在这里从控制台读取,这里的另一种方法使用“扫描仪”从控制台读取:
import java.util.Scanner;
String data;
Scanner scanInput = new Scanner(System.in);
data= scanInput.nextLine();
scanInput.close();
System.out.println(data);
有从控制台/键盘读取输入的字符串几种方法。以下示例代码显示了如何使用Java从控制台/键盘读取字符串。
public class ConsoleReadingDemo {
public static void main(String[] args) {
// ====
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter user name : ");
String username = null;
try {
username = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("You entered : " + username);
// ===== In Java 5, Java.util,Scanner is used for this purpose.
Scanner in = new Scanner(System.in);
System.out.print("Please enter user name : ");
username = in.nextLine();
System.out.println("You entered : " + username);
// ====== Java 6
Console console = System.console();
username = console.readLine("Please enter user name : ");
System.out.println("You entered : " + username);
}
}
代码的最后部分使用java.io.Console
类。当通过Eclipse运行演示代码时,您无法从System.console()
获取Console实例。因为eclipse将您的应用程序作为后台进程运行,而不是作为系统控制台的顶级进程运行。
以下内容取athspk's answer并将其合并为一个循环,直到用户输入“exit”。我也写了一个followup answer,我已经把这段代码做了测试。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LoopingConsoleInputExample {
public static final String EXIT_COMMAND = "exit";
public static void main(final String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter some text, or '" + EXIT_COMMAND + "' to quit");
while (true) {
System.out.print("> ");
String input = br.readLine();
System.out.println(input);
if (input.length() == EXIT_COMMAND.length() && input.toLowerCase().equals(EXIT_COMMAND)) {
System.out.println("Exiting.");
return;
}
System.out.println("...response goes here...");
}
}
}
输出示例:
Enter some text, or 'exit' to quit
> one
one
...response goes here...
> two
two
...response goes here...
> three
three
...response goes here...
> exit
exit
Exiting.
尝试此。希望这会有所帮助。
String cls0;
String cls1;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
cls0 = in.nextLine();
System.out.println("Enter a string");
cls1 = in.nextLine();
我写的Text-IO库,它可以处理System.console(的问题),从IDE中运行的应用程序时,被空。
它引入了一个抽象层,类似于McDowell提出的抽象层。 如果System.console()返回null,则该库将切换到基于Swing的控制台。
此外,文本IO具有一系列的有用的功能:
用例:
TextIO textIO = TextIoFactory.getTextIO();
String user = textIO.newStringInputReader()
.withDefaultValue("admin")
.read("Username");
String password = textIO.newStringInputReader()
.withMinLength(6)
.withInputMasking(true)
.read("Password");
int age = textIO.newIntInputReader()
.withMinVal(13)
.read("Age");
Month month = textIO.newEnumInputReader(Month.class)
.read("What month were you born in?");
textIO.getTextTerminal().println("User " + user + " is " + age + " years old, " +
"was born in " + month + " and has the password " + password + ".");
在this image你可以看到一个基于Swing的控制台中运行上面的代码。
这是android吗? (我从你的用户ID猜测) – 2011-01-10 07:10:32
看看McDowell的项目“AbstractingTheJavaConsole”:http://illegalargumentexception.googlecode.com/svn/trunk/code/java/AbstractingTheJavaConsole/ – athspk 2011-01-12 01:24:10
@RyanFernandes他的名字如何与他的问题? – 2013-05-01 04:29:58