2017-01-07 6 views
0

だから私は、これは文字列としてユーザー入力を取得し、これら二つのことを確認するためのパスワードを確認するために取得しようとしている:のJavaトラブルがチェック

  1. パスワードは最低であります8文字。
  2. パスワードには文字と数字のみが含まれています。

これは問題です。 パスワードを8文字以上確認しても、文字と数字だけが含まれていることを確認しても機能しません。最小の数字/文字数が入力された場合、単一のメッセージを出すことなく終了するだけです。それは文字または数字ではない文字を見ればしかし、それはこのをプリントアウトします:

パスワードを入力してください:###

パスワードは文字と数字のみを含めることができます。

パスワードには文字と数字のみを使用できます。

パスワードには文字と数字のみを使用できます。

パスワードを受け入れてください!

出力とは何か、それはすべきこの:###

パスワードは文字と数字のみを含めることができます。

パスワードを入力してください。

または

パスワードを入力してください:受け入れtest1234

パスワードを!

password.java  
    package Password; 
    import java.util.Scanner; 

    public class Password { 

    public static void main(String[]args) 
    { 

    Scanner input = new Scanner (System.in); 
    boolean valid = true; 
    System.out.println("Please enter a password:"); 
    String password = input.nextLine(); 
    int i = 0; 
    //declares i as the counter varible to control the loop and initializes it to 0 

    if((password.length() < 8)) //check the passwords length and make sure it's a minimum of 8 characters 
    { 
    System.out.println("Password must have at least 8 characters."); 
    valid = false; 
    } 
    //loops the code below it for the length of i until the password length is reached 
    while(i < password.length()) 
    { 
    if ((password.charAt(i)>='a' && password.charAt(i)<='z') || (password.charAt(i)>='A' && password.charAt(i)<='Z') ||(password.charAt(i)>='0' && password.charAt(i)<='9')) 
    //loop through all the characters in the string entered and make sure they only consist of letters and numbers 
     valid = true; 
    else 
    { 
     System.out.println("Password can only contain letters and numbers."); 
     valid = false; 
    } 
    i++; 
    //add an iteration to the loop 
    } 

    if(!valid == true) 
    System.out.println("Password accepted!"); 
    } 
    } 

これに関するすべてのヘルプは素晴らしいものです。

+0

これは役立ちますか?http://stackoverflow.com/questions/13674449/checking-password-code? – ppasler

+1

無効な文字を見つけたら、validをfalseに設定してループから抜け出す必要があります。それ以外の場合は、フラグを有効にして再度trueに設定するリスクがあります。 –

答えて

0

コードを少し簡略化するには、まずvalidで始まり、password.length()をチェックします。パスワード内の各文字をテストします(無効な文字があれば停止します)。次に、受け入れたメッセージを表示する前に、パスワードが有効かどうかを確認します。あなたは間違った文字を見たときと同様に、

Scanner input = new Scanner(System.in); 
System.out.println("Please enter a password:"); 
String password = input.nextLine(); 
boolean valid = password.length() >= 8; 

if (!valid) { 
    System.out.println("Password must have at least 8 characters."); 
} else { 
    for (char ch : password.toCharArray()) { 
     if (!Character.isLetter(ch) && !Character.isDigit(ch)) { 
      System.out.println("Password can only contain letters and numbers."); 
      valid = false; 
      break; 
     } 
    } 
} 
if (valid) { 
    System.out.println("Password accepted!");   
} 
0

は、このチェックコードの主なエラーは、whileループでループを継続する必要はありません、単にあなたがそのようにチェックし、この種の操作を行います。

String toCheck;  //the string to check some criteria 
boolean valid = true; // we assume that nothing wrong happen till now 

for(int i=0;i<toCheck.length();i++){ //loop over the characters 
    if(/*condition of wrong case*/){ 
     valid = false;    //mark that something wrong happen 
     break;      //exit the loop no need to continue 
    } 
} 

if(valid){ 
    //nothing wrong happen 
} else { 
    //something wrong happen 
}