Marcus
Marcus

Reputation: 115

How to turn a boolean into a message?

Basically the program i made is for a password that limits things to 6-10 characters and requires at least 1 character and 1 password. How do I take the boolean and turn it into a message. I'm using another class for Japplet UI which is why I want to pass this on.

Edit: I dont understand how to do this when my other class gets user input.

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;



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

        {


        String message = "";

        if( Password.check(null) == true)          This is what I'm having trouble whit
        {
            message = "The Password meets the requirements";
        }
        else
        {
        message =   "Your password does not match the requirements";
        }


            return message;
        }
    }

Upvotes: 1

Views: 111

Answers (2)

Rahul
Rahul

Reputation: 45070

You need to check your password and accordingly assign your message.

if( Password.check(null) == true) // This just passes null instead of the password string to the check method.

You need to send the actual password string to the check method, instead of null.

if( Password.check(yourPasswordString) == true) // Send your password value got from the user

To simplify it even more, you can remove the == check.

if( Password.check(yourPasswordString))

And to simplify it even further, you can use the ternary operator as David mentioned and something like this

String myMessage = Password.check(yourPasswordString) ? "Your password is valid" : "You failed";

Edit:

After seeing your edit, this is what you need to do.

Pass the boolean result to the getDisplay() method.

// inside init() method
boolean pass= Password.check(userInput);
Password mess= new Password();
passwordLabel.setText(mess.getDisplay(pass)); // passing the boolean to the getDisplay method
...
// getDisplay method.
public String getDisplay(boolean result) {
    return result ? "The Password meets the requirements" : "Your password does not match the requirements";
}

You could even make your getDisplay() as a static method and call it like this.

// inside init() method
boolean pass= Password.check(userInput);
// static method, hence mess object is not required.
passwordLabel.setText(Password.getDisplay(pass)); // passing the boolean to the getDisplay method
...
// getDisplay method.
public static String getDisplay(boolean result) { // static method
    return result ? "The Password meets the requirements" : "Your password does not match the requirements";
}

Upvotes: 2

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

Write something like

String myMessage = result ? "Your password is valid" : "You failed";

The question mark means - look at the boolean expression on the left. If it's true, return whatever's next; if it's false, return what comes after the colon.

Upvotes: 2

Related Questions