检查字符串是否为空或空格 [英] Checking the string for null or empty space

查看:154
本文介绍了检查字符串是否为空或空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一个文本文件abc .txt,其中的标签分隔如下所示

I am reading a text file abc .txt which is tab delimited as shown below

gfh  hgh  thf
---  ---  ---
fgh  sji  irj   
rhf  dhh  fhf
kji  idj  ddt

我能够成功读取它(对于表中的所有列我已经开发了一个单独的pojo以及getter和setter),优点是我可以获得完整行的值通过它的getter。

and I am able to read it successfully (for all the columns in the table I have developed a separate pojo along with getters and setters), the advantage is that I can get the value of complete row through its getters.

现在我必须确保表中没有列应该为null,如果任何列值为null,那么我应该抛出一个新的异常,例如..

Now I have to make sure that no column in the table should be null and if any column value is null, then I should throw a new exception, for example ..

gfh  hgh  thf
---  ---  ---
fgh  sji  irj   //row 1
rhf  dhh  fhf
kji  idj  ddt
fgq       fio   //As seen in this row that second column value is null 

现在,我所遵循的方法是在字符串中明确获取值,如下所示

Now the approach that I am following is that getting the value row wise in a string as shown below

String n = f.getgfh()+f.gethgh()+f.getthf();  //it will contain the contents of the row 1

制作一个单独的方法并将传递此字符串

make a separate method and will pass this string

private boolean validaterow(String f)
{
}

在这个方法中,我在字符串中取完整行,在这个方法中,我想在标记中打破这个字符串,然后进一步评估这些标记for empty或null,如果有,那么我将返回false

in this method here I am taking the complete row in a string and inside this method I want to break this string in tokens and then further evaluate those tokens for empty or null , if there are then I will return false

推荐答案

试试这个,

private boolean validateRow(String f)
{
if(f.getgfh() != null && !f.getgfh().trim().isEmpty() &&
 f.gethgh() != null && !f.gethgh().trim().isEmpty() &&
            f.getthf() != null && !f.getthf().trim().isEmpty())
    {
        return true;
    }
    else
    {
        return false;
    }
}

这篇关于检查字符串是否为空或空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆