将布尔值转换为消息?

时间:2013-11-18 07:39:08

标签: java

此程序获取用户密码并告诉他们是否有有效密码。密码需要6-10个字符,其中至少包含1个数字和字母。我在PasswordUI类中获得用户输入。我的密码类用它来检查密码是否有效并返回一个布尔值。如何获取该布尔值并使用它来形成我将在我的PasswordUI类中使用的消息?

public class PasswordUI extends JApplet implements ActionListener {

    JLabel passwordLabel = new JLabel("");
    JTextField inputBox = new JTextField(40);
    JButton goButton = new JButton("Go");
    Container con = getContentPane();

    public void init()
    {
        con.setLayout(new FlowLayout());
        con.add(new JLabel("Enter a password between 6-10 characters. Must contain atleast 1 number and 1 letter"));
        con.add(inputBox);
        con.add(goButton);
        con.add(passwordLabel);

        goButton.addActionListener(this);
        inputBox.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
        String userInput = inputBox.getText();

        boolean pass= Password.check(userInput);
        //String message = Password.display(null);
        Password mess= new Password();

        passwordLabel.setText( mess.getDisplay());



    }
}

public class Password {


    public static boolean check(String str)
    {

    boolean result = false;
    char ch;
    int letterCount = 0,
            digitCount= 0 ;
    int length = str.length();

    if(str.length()>= 6)

    {
        if(str.length()<= 10)
        {
            for (int i=0; i<length; i++ ) //i<end 
            {
                ch = str.charAt(i);
                if (Character.isDigit(ch))
                    digitCount++;
                else if (Character.isLetter(ch))
                    letterCount++;
            }

            if (letterCount >= 1 && digitCount >= 1)
                result = true;

            }
        }
    return result;
    }


    public String getDisplay()//how do I do this?

    {
        String message = result ? "Your password is valid" : "You Password is invalid"; 
//result isn't working


        return message;
    }
}

3 个答案:

答案 0 :(得分:1)

将布尔变量result传递给getDisplay()方法。

public static String getDisplay(boolean result) {         
    return result ? "Your password is valid" : "You Password is invalid";
}

答案 1 :(得分:1)

在你的actionPerformed中,你会收到密码检查的结果,但你在新的密码混乱中调用getDisplay。

public void actionPerformed(ActionEvent e)
    {
        String userInput = inputBox.getText();

        boolean pass= Password.check(userInput);
        //String message = Password.display(null);
        Password mess= new Password();

        passwordLabel.setText( mess.getDisplay());
    }

这可以解决您的问题:

public void actionPerformed(ActionEvent e)
    {
        String userInput = inputBox.getText();

        boolean pass= Password.check(userInput);
        //String message = Password.display(null);
        **//**Password mess= new Password();

        passwordLabel.setText(**pass**);
    }

但是,我建议您重新构建您的密码类。让它有一个字段String pass,然后只需覆盖toString()并使其返回(String)check(pass)。

答案 2 :(得分:0)

我明白了。这是我试图做的解决方案

public void actionPerformed(ActionEvent e)
    {
        String userInput = inputBox.getText();
        Password mess= new Password();//**added this
        boolean pass= mess.check(userInput);//**used this
        passwordLabel.setText(mess.getDisplay(pass));//**changed this



    }
    }
public  boolean check(String str) //**removed static
    {

    boolean result = false;
    char ch;
    int letterCount = 0,
            digitCount= 0 ;
    int length = str.length();

    if(str.length()>= 6)

    {
        if(str.length()<= 10)
        {
            for (int i=0; i<length; i++ ) //i<end 
            {
                ch = str.charAt(i);
                if (Character.isDigit(ch))
                    digitCount++;
                else if (Character.isLetter(ch))
                    letterCount++;
            }

            if (letterCount >= 1 && digitCount >= 1)
                result = true;

            }
        }
    return result;
    }


    public String getDisplay(boolean result) //**changed this to boolean result as was suggested

    {
        String message = result ? "Your password is valid" : "You Password is invalid"; //how do I do this?


        return message;
    }