2010-10-06 59 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