我用readLine的BufferedReader获得输入/从用户的新密码,但希望屏蔽密码,所以我试图用java.io.Console类.问题是在Eclipse中调试应用程序时System.console()返回null.我是Java和Eclipse的新手,不确定这是最好的实现方式吗?我右键单击源文件并选择"Debug As">"Java Application".有没有解决方法?
当我尝试使用Scanner以下方法获取用户输入时,我遇到了一个问题:
import java.util.Scanner;
public class Main
{
public static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("Insert a number: ");
int number = input.nextInt();
System.out.print("Text1: ");
String text1 = input.nextLine();
System.out.print("Text2: ");
String text2 = input.nextLine();
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Insert a number: 55
Text1: Text2: Hi there!
Run Code Online (Sandbox Code Playgroud)
如您所见,该程序已跳过String text1 = input.nextLine();.这里有什么问题?以及如何解决这个问题?
我见过一些代码如:
out.println("print something");
Run Code Online (Sandbox Code Playgroud)
我试过了 import java.lang.System;
但它不起作用.你怎么用的out.println()?
我用来java.util.Scanner从控制台读取命令.
try
{
ICommand cmd = cmdReader.ReadCommand();
cmd.Execute(conn);
}
catch(MyException ex)
{
// print a message about unknown command
continue;
}
Run Code Online (Sandbox Code Playgroud)
在哪里ReadCommand实施如下:
try (Scanner scanIn = new Scanner(System.in))
{
if (!scanIn.hasNextLine()) return null;
line_ = scanIn.nextLine();
ICommand command = parser_.ParseCommand(line_);
return command;
}
Run Code Online (Sandbox Code Playgroud)
在第一次迭代中代码工作正常,我写了一些无效的东西(不是命令),代码打印警告并继续.但即使我在控制台中写东西,其他迭代也会返回null此处if (!scanIn.hasNextLine()) return null;.看起来java.util.Scanner没有看到输入.难道我做错了什么?然后我怎么可以等待用户输入(不想使用循环sleep)?