import java.io.Console;public class NewConsole {
public static void main(String[] args)
{
Console c = System.console();
String name="d";
char[] pw = c.readPassword();//这句抛出异常但不知怎样改
for(char ch: pw)
c.format("%c ", ch);
c.format("\n");
MyUtility mu = new MyUtility();
while(true)
{
name = c.readLine("%s", "input?: ");
c.format("output :%s \n",mu.doStuff(name));
}
}
}class MyUtility
{
String doStuff(String arg1)
{
return "result is "+arg1;
}
}
public static void main(String[] args)
{
Console c = System.console();
String name="d";
char[] pw = c.readPassword();//这句抛出异常但不知怎样改
for(char ch: pw)
c.format("%c ", ch);
c.format("\n");
MyUtility mu = new MyUtility();
while(true)
{
name = c.readLine("%s", "input?: ");
c.format("output :%s \n",mu.doStuff(name));
}
}
}class MyUtility
{
String doStuff(String arg1)
{
return "result is "+arg1;
}
}
public class NewConsole {
public static void main(String[] args)
{
Console c = System.console();
String name="d";
if(c==null){
System.out.println("C未获取到,导致下面那句报错!");
}
char[] pw = c.readPassword();//这句抛出异常但不知怎样改
for(char ch: pw)
c.format("%c ", ch);
c.format("\n");
MyUtility mu = new MyUtility();
while(true)
{
name = c.readLine("%s", "input?: ");
c.format("output :%s \n",mu.doStuff(name));
}
}
}
public static Console console()返回与当前 Java 虚拟机关联的唯一 Console 对象(如果有)。 返回:
系统控制台(如果有),否则返回 null。
从以下版本开始:
1.6
System.console()有可能返回空值
童鞋可以去看api..
用cmd 下运行程序一点问题没有
Console c = System.console();测试c一下是否为空,如果为空就返回,如果不为空进行读数据
if(c!=null) 加上