简体   繁体   中英

check if string and int entered in constructor

I am trying to learn java and I am having some issues with my code. I am trying to use an if statement to check there has been any input into the constructor. If the email String and the membership number have been entered, the check logged in status method should return that the user is logged in. other wise it should return they are not logged in.

public class Member
{
    // varibales declaration 
    private String email;  
    private int membershipNumber; 
    private boolean loggedInStatus; 

    /**
     * Constructor for objects of class Member
     */
    public Member(String memberEmail, int newMembershipNumber )
    {
        // initialise instance variables
        email = memberEmail;
        membershipNumber = newMembershipNumber;
    }

    //loggedInStatus method
    public void setloggedInStatus() 
    { 
        if ( email == email && membershipNumber == membershipNumber ) {

            System.out.println("you are logged in ");
        } else {
            System.out.println("you are not logged in");
        }
    }
} 

If there hasn't been any input, both variables will contain default values ( null for a String, 0 for an integer.

You could check that:

public void setloggedInStatus() {
    if (email != null && membershipNumber != 0) {
        System.out.println("you are logged in ");
    } else {
        System.out.println("you are not logged in");
    }
}

But in your case it's not possible to call the constructor without an argument for email and membershipNumber . Both will always have a value, their values just might be null and 0 . You have to decide if that makes sense in your case and if an expression like if (email != null && membershipNumber != 0) is enough to check both values for validation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM