Marcus
Marcus

Reputation: 115

Turning boolean into a message?

This program takes a users password and tell them if they have a valid password. The password needs 6-10 characters, which contain at least 1 number and letter. I get user input in my PasswordUI class. My Password class uses that to check if the password is valid and returns a boolean. How do I take that boolean and use it to form a message that I will use in my PasswordUI class?

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;
    }
}

Upvotes: 0

Views: 105

Answers (3)

Marcus
Marcus

Reputation: 115

I figured it out. This is the solution to what I was trying to do

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;
    }

Upvotes: 0

aga
aga

Reputation: 29416

Pass the boolean variable result into the getDisplay() method.

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

Upvotes: 1

Ron
Ron

Reputation: 1508

In your actionPerformed you receive the result of the password check, but you call getDisplay on a new Password mess.

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());
    }

This would solve your problem:

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**);
    }

I do, however, suggest you restructure your Password class. Make it have a field String pass, then simply override toString() and make it return (String) check(pass).

Upvotes: 1

Related Questions