关于JAVA的一个问题

来源:百度知道 编辑:UC知道 时间:2024/09/23 02:31:25
在命令行下,运行程序时接受用户输入,怎么样使用户的输入用"*"显示.

jdk6里有个新类Console可以达到这个效果(没有回显--连星号都不显示)也许对你有帮助:(注意下面的这个程序必须在命令行下运行,即编译后用java MainClass来运行,不能在eclipse下运行)

import java.io.Console;
import java.sql.SQLException;

public class MainClass {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Console console = System.console();
if (console == null) {
System.err.println("sales: unable to obtain console");
return;
}

String password = new String (console.readPassword ("Enter password: "));
System.out.println(password);
}
}

在DOS状态下,你想输入的内容则"*"显示,我可以告诉你,这不太可能.别想了,你们功能还是用其它的方式来实现吧.

String str=null;//定义个接受输入标准输入

BufferedReader stdIn=new Bufferedreader(new InputSreamReader(System.in));

try//用到流时需要抛出异常

{

str=stdIn.readLine();//读入一行

//如果想要得到个数字,可以通过下面的转换

//int a=Integer.parseInt(str);