简体   繁体   English

检查构造函数中是否输入了字符串和整数

[英]check if string and int entered in constructor

I am trying to learn java and I am having some issues with my code.我正在尝试学习 java,但我的代码有一些问题。 I am trying to use an if statement to check there has been any input into the constructor.我正在尝试使用 if 语句来检查构造函数中是否有任何输入。 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.如果输入了 email 字符串会员编号,则检查登录状态方法应返回用户已登录。否则应返回用户未登录。

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.如果没有任何输入,则两个变量都将包含默认值(字符串为 null, null0

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 .但是在您的情况下,如果没有emailmembershipNumber的参数,则无法调用构造函数。 Both will always have a value, their values just might be null and 0 .两者都会有一个值,它们的值可能是null0 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.您必须确定这在您的情况下是否有意义,以及if (email != null && membershipNumber != 0)类的表达式是否足以检查两个值以进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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