Reputation: 121
I have an array list in which objects are stored, and from those object's getters I get string value as shown below
List<abcd> hgfer = (List<abcd>)Getter.rows(jhfile);
for(abcd f: hgfer)
{
String p = f.getFromArea()
As shown above the array list and the values that I am extracting. Now I have to make sure that the string that I am getting is not empty plus it should be trimmed, I achieved this as shown below:
p.getFromArea().trim().length() > 0
Now there are several getters attached to this object which will return a string. For every individual string I have to do this. I was thinking to make a separate individual method which will return a boolean value and a string parameter will be pass.For ex:
private String validaterow(String g)
{
boolean valid = false;'
try{
**//code to check that should not be empty plus it should be trim one**
}
catch(){}
valid = false;
}
and I have to make a call to this method somewhere within the class
List<abcd> hgfer = (List<abcd>)Getter.rows(jhfile);
for(abcd f: hgfer)
{
if (!validaterow(f.getFromArea())
{//customised message
}
else
continue;
Now please advise how could I achieve that string should not be empty plus it should be trim one
Upvotes: 0
Views: 607
Reputation: 327
Use StringUtils class from apache.commons.lang
If (StringUtils.isNotBlank(yourString)){
isValid = true;
}
This will trim the yourString and check for null or blank value.
Upvotes: 0
Reputation: 6982
According to Apache Commons, you could use theire method to check, if a string is empty or not.
/**
* <p>Checks if a String is whitespace, empty ("") or null.</p>
*
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is null, empty or whitespace
* @since 2.0
*/
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
The reason for this way is demonstrated in this example:
System.out.println(Character.isWhitespace('c')); // false
System.out.println(Character.isWhitespace(' ')); // true
System.out.println(Character.isWhitespace('\n')); // true
System.out.println(Character.isWhitespace('\t')); // true
Upvotes: 1
Reputation:
Try this:
private boolean validaterow(String g){
boolean isValid = false;
if(g.trim().isEmpty()){
isValid = true;
}
return isValid;
}
If the parameter String is not empty the method will return false.
Upvotes: 0
Reputation: 28558
try this:
private boolean validaterow(String text)
{
try{
return (text == null) || (text != null && text.trim().length() == 0);
}
catch(){}
return false;
}
}
Upvotes: 0
Reputation: 1677
boolean validateNullString(String str)
{
return (str == null || str.trim().length() == 0);
}
This will validate your String object against null and empty.
Upvotes: 0
Reputation: 45070
You can try something like this:-
public boolean isNullOrEmpty(String str) {
return (str == null) || (str.trim().length() == 0);
}
This will return true
if your String
is null
or empty
, else false
.
Upvotes: 1