【问题标题】:cannot get correct password format无法获取正确的密码格式
【发布时间】:2013-10-11 16:16:41
【问题描述】:

我想创建一个应用程序,要求用户从输入对话框中输入密码。密码必须少于 10 个字符且多于 6 个字符。此外,密码应包含至少一个数字或字母。当密码满足所有要求时,要求用户再次输入密码,在第二个密码与第一个密码匹配之前不要让用户继续。 我的代码如下,但它不检查字母或数字。 有人可以帮忙吗?

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;

public class Password{

public static void main(String[] args){

String firstPassword="";
String secondPassword="";

char charChecked;

boolean letter = false;
boolean digit = false;

int len=firstPassword.length();

firstPassword= JOptionPane.showInputDialog("ENter");

if (( len<6 || len>10) || ! (Character.isLetterOrDigit(firstPassword.charAt(len))) )

{firstPassword= JOptionPane.showInputDialog("Enter the Correct Format of password");
}


if ((len>=6|| len<=10) || (Character.isLetterOrDigit(firstPassword.charAt(len))) )
do
{
secondPassword= JOptionPane.showInputDialog("Please enter again the password to confirm");
}
while (!(firstPassword.equals(secondPassword)));
if (firstPassword.equals(secondPassword))
{
JOptionPane.showMessageDialog(null,"Accepted");
}

}
}

【问题讨论】:

    标签: java passwords character


    【解决方案1】:
    int len=firstPassword.length();
    
    firstPassword= JOptionPane.showInputDialog("ENter");
    

    你的代码被反转了……你需要在输入后调用长度。

    firstPassword= JOptionPane.showInputDialog("ENter");
    int len=firstPassword.length();
    

    试着说点什么……

    【讨论】:

    • 我更正了。它无法正常工作并让我超出界限异常。我认为“(Character.isLetterOrDigit(firstPassword.charAt(len)”)有错误
    • 很抱歉现在才回答...但是是的,因为您正在尝试与最后一个位置进行比较。对于您查看您的密码是否包含字母或数字,您需要一个 cicle。 For( int i = 0; i
    猜你喜欢
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    相关资源
    最近更新 更多