2010-10-06 67 views
0

我开发了以下应用程序,在用户输入错误的PIN三次后,我需要屏蔽PIN并终止程序。但是,只有当我在开始时关闭stopThread(我在下面的代码中注释它)时,程序才会终止,但是当我这样做时,所有三个通道都不会发生密码遮罩。但是,在显示登录成功画面之前关闭stopThread时,程序不会终止。我需要使用ctrl + c来结束程序。密码屏蔽不会在需要时终止程序

任何帮助,非常感谢。

boolean stopThread = false; 
boolean hideInput = false; 
boolean shortMomentGone = false; 
public static double userBal=0.0D; 

public void run(){ 
    try{ 
     sleep(500); 
    } catch(InterruptedException e){ 
    } 
    shortMomentGone = true; 
    while(!stopThread){ 
     if(hideInput){ 
      System.out.print("\b*"); 
     } 
     try{ 
      sleep(1); 
     } catch(InterruptedException e){ 
     } 
    } 
} 

public static final int NB_OF_TRIES = 3;   

public void validatePin(){ 
    BankAccount getAll=new BankAccount(); 
String pin=""; 
    getAll.Login(); 
    Login hideThread =new Login(); 
    hideThread.start(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    try{  
    do{ 

     } while(hideThread.shortMomentGone == false );   
    // Now the hide thread should begin to overwrite any input with "*" 
     hideThread.hideInput = true;   // Read the PIN 
     System.out.println("\nPIN:"); 

    boolean pinMatch = false; 
     int i = 0; 

    while(!pinMatch && i < NB_OF_TRIES) { 
     hideThread.hideInput = true; 
     pin = in.readLine(); 
    i++; 
     //hideThread.stopThread = true;  //Program terminates after third attempt 
               //PIN masking is stopped, if uncommented 
     System.out.print("\b \b");   
     if(pin.equals(" ")){ 
    System.out.println("Please do not leave unnecessary spaces!"); 
    getAll.Login(); 
    }else if(pin.equals("")){ 
    System.out.println("Please do not press the enter key without entering the PIN!"); 
     getAll.Login(); 
    } 

    FileInputStream fileinputstream = new FileInputStream(".\\AccountInfo.txt"); 
     DataInputStream datainputstream = new DataInputStream(fileinputstream); 
     BufferedReader bufferedreader1 = new BufferedReader(new InputStreamReader(datainputstream)); 

    do 
     { 
      String s1; 
      if((s1 = bufferedreader1.readLine()) == null) 
      { 
       break; 
      } 
      if(s1.trim().charAt(0) != '#') 
      { 
       String as[] = s1.split(" "); 
       if(pin.equals(as[0])) 
       {   
        System.out.println("You have login!"); 
        String s2 = as[2]; 
        userBal = Double.parseDouble(s2);      
        getAll.balance = userBal; 
       hideThread.stopThread = true; 
        getAll.MainMenu(); 
     System.exit(0); 
       }else if(pin != as[0]){ 
     System.out.println("Invalid PIN!"); 
     getAll.Login();   
     System.out.println("\n NOTE :- You are only allowed to enter the PIN THREE times. The number of tries remaining before your card is blacklisted are "+i + "\n Please re-enter your PIN"); 
       } 
      } 
     } while(true); 
     datainputstream.close();  
    }//End of While Loop 

    }catch(Exception exception) 
    { 
     System.err.println((new StringBuilder()).append("Error: ").append(exception.getMessage()).toString()); 
    }//End of try-catch block  
} 

回答

2

java.io.Console中有一个readPassword()方法,使用它。为什么你需要一个单独的线程呢?这使得一切都变得过于复杂。

关于你的问题,为什么这并不紧密:Java的可优化while(isTrue){}喜欢的东西if(isTrue) { while(true) { } },如果你不设置isTruevolatile或同步访问IsTrue运算(的getter/setter)。这种优化称为提升,并在有效Java SE项目66中进行了解释。

这里有一篇文章解释了您的问题:echo *而不是空格。 http://java.sun.com/developer/technicalArticles/Security/pwordmask/ 他们也走了复杂的路,但它的工作。我宁愿将空白置于星号之后,因为这是更简单的方法。不回显*是* nix标准afaik。

+0

实际上我使用了readPassword方法,但是它用空格替换了字符,但我需要它来显示一个公共符号。这就是我使用这种技术的原因 – Yoosuf 2010-10-06 11:35:27

+0

整个解决方案太复杂了。 – 2010-10-06 11:37:45

0

实际上,在我分析它之后,我意识到系统不会终止的原因是因为它没有保存在适当的位置。因此,解决方案是尽快结束程序,while循环关闭,然后一切都会正常工作。

 } while(true); 
     datainputstream.close(); 
}//End of While Loop 
    System.exit(0); // After the system is closed the program would terminate after the third attempt 
    }catch(Exception exception) 
    { 
     System.err.println((new StringBuilder()).append("Error: ").append(exception.getMessage()).toString()); 
    }//End of try-catch block