1

I'm new to java and i want to do a condition check and return a message

PersonalDetails Request: It holds the value for all the below infos

getID();

getName();

getDesignation();

getHomeAddress();

getOfficeAddress();

getEmailID();

getMobile();

getHomePhone();

getOfficePhone();

i want to check all values for empty then return message.

Like "Your ID,Name, Mobile cannot be empty" if i pass empty values to ID, Name, Mobile

Below is the sample snippet which has to do the check for all PersonalDetails Request

public static String checkValue(PersonalDetails Request) {
    String str="Your ";
    if(request.getID().isEmpty())
    {
         str="ID,";
    }
    if(request.getName().isEmpty())
    {
     str="Name";
    }
if(request.getDesignation().isEmpty())
    {
     str="Designation";
    }
if(request.HomeAddress.isEmpty())
    {
     str="Address";
    }
   str+= "cannot be empty"

    return str;
}

Is this right or any other easy approach will address the issue

Thanks in advance

1
  • why don't you pass the values as parameterized constructor arguments, there you can check the values... Commented Jun 30, 2015 at 4:27

4 Answers 4

2

No if the string contains null then it will through a null pointer exception.

You first need to check it for null then for Empty.

Can we rely on String.isEmpty for checking null condition on a String in Java?

Sign up to request clarification or add additional context in comments.

Comments

0

There are multiple suggestions for your code:

  1. Use camel-case for variables, methods etc. in your code. For example parameter should be named as PersonalDetails request and not PersonalDetails Request. These are standard coding conventions. Also getter/setter should follow the rules, check HomeAddress which seems to miss it.
  2. Use StringBuilder class as it performs better specially in case of appending while in a loop.
  3. You need to check for null before performing any operation else you will be facing a NullPointerException. You can also read about Optional in Java 8.

The code can be improved like:

public static String checkValue(PersonalDetails request) {
        if(null == request ) {
            //Throw exception or log/return message as per your need.
        }

        int some_appropriate_size = 50; // You need to decide about some_appropriate_size so that it starts with enough capacity for the full content we are going to append.
        StringBuilder stringBuilder = new StringBuilder(some_appropriate_size);

        stringBuilder.append("Your ");
        if(null!= request.getID() && request.getID().isEmpty())
        {
            stringBuilder.append("ID,");
        }
        if(null!= request.getName() && request.getName().isEmpty())
        {
            stringBuilder.append("Name");
        }
        if(null!= request.getDesignation() &&request.getDesignation().isEmpty())
        {
            stringBuilder.append("Designation");
        }
        if(null!= request.getHomeAddress() && request.getHomeAddress().isEmpty())
        {
            stringBuilder.append("Address");
        }
        stringBuilder.append( "cannot be empty");

        return stringBuilder.toString();
    }

4 Comments

Can you please confirm if this is a static method or instance method ? public static String checkValue(PersonalDetails request)
The method declaration itself specifies the method is static. If it were instance method it would have been declared like:public String checkValue(PersonalDetails request)
No. My question was this functionality can be in a static method or it has to be an instance method. i'm confused to make a decision. Please advise
The method works on an object of class PersonalDetails and may not have any usage for other classes, so making it static wont be useful. IMO you should make it an instance method.
0

Your condition is correct but you forget to concat string in each if. If don't concate it will not have existing value but new value only.

  public static String checkValue(PersonalDetails Request) {
String str="Your ";
if(request.getID().isEmpty())
{
     str= str + "ID,";
}
if(request.getName().isEmpty())
{
 str= str + "Name";
}
if(request.getDesignation().isEmpty())
{
 str= str + "Designation";
}
if(request.HomeAddress.isEmpty())
{
 str= str + "Address";
}
   str+= "cannot be empty"

return str;
}

My suggestion will be to use StringBuilder to get better performance.

Comments

0

Here it's better to check not only isEmpty. It's better to check null, " " or "-" as well.

Before you check isEmpty or "-" or " ", make sure to check null to avoid getting NullPointerException

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.