检查字符串是否为空或空格

时间:2013-05-02 09:24:01

标签: java string io

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

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

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

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

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 

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

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

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

private boolean validaterow(String f)
{
}

在这个方法中,我在一个字符串中取完整行,在这个方法里面我想在令牌中打破这个字符串,然后进一步评估那些令牌为空或null,如果有,那么我将返回false

7 个答案:

答案 0 :(得分:1)

试试这个,

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;
    }
}

答案 1 :(得分:0)

您可以像在getters中一样设置空检查

public String gethgh()    {

  return (String) ((null == hgh) ? new UserDefinedException() : hgh); 

}

此处必须将UserDefinedException声明为类

我认为这应该有用

答案 2 :(得分:0)

为什么要将它传递给方法?

使用

String row1,row2,row3;
row1=f.getgfh();
row2=f.gethgh();
row3=f.getthf();

您可以在连接时检查您的情况。

if(row1==null)
 'throw your message
else
 row1=""
if(row2==null)
 'throw your message
else
 row2=""
if(row3==null)
 'throw your message
else
 row3=""

最后,

String n = row1+row2+row3;

答案 3 :(得分:0)

使用StringIsNullOrEmtpy检查行是否对该行的每个条目都有效

private boolean ValidateRow()
{
   return !(StringIsNullOrEmpty((f.getgfh())) || 
            StringIsNullOrEmpty((f.gethgh())) ||
            StringIsNullOrEmpty((f.getthf()))
            );
}

private bool StringIsNullOrEmtpy(string input)
{
   return input.isEmpty() || input == null;
}

答案 4 :(得分:0)

假设您有一个方法getNextValueAndPosPair(),它返回您的tsv(制表符单独值)文件的下一个值以及读取值后找到的最后一个空格的位置(ig a Tab 换行符)。

然后,您可以通过chechking验证输入行,它确实包含由 Tab 分隔的三个不同值。

在实施之下:

// A simple general pair class.
class Pair<A, B> {

    Pair(A first, A second) {
        this.first = first;
        this.second = second;        
    }

    public A first() { return first; }


    public A second() { return second; }


    private final A first;

    private final B second;
}

// Returns true if this rows contains 4 distinct values, false otherwise.
private boolean validaterow(String f) {
    int pos = 0;
    for (int i = 0; i < 3; i++) {
        Pair<String, Integer> valueAndPos = getNextValueAndPosPair(f, pos);
        String value = valueAndPos.first();
        int pos = valueAndPos.second();
        if (value.isEmpty()) {
            System.err.println("The " + i + "-th value is null.");
            return false;
        }
        pos += 1;
    }
    return true;
}

// Returns a Pair holding the next tsv value in the row and the position of the delimiter space
private Pair<String, Integer> getNextValueAndPosPair(String f, int start) {
    int length = 0;
    for (int i = start; i < f.length && !Character.isSpaceChar(f.charAt(i)); i++) length++;

    int end = start + length;
    String value = f.substring(start, end);
    Pair<String, Integer> valueAndPos = new Pair<>(value, end);
    return valueAndPos;
}

答案 5 :(得分:0)

看看apache string utils库。它可能有你需要的东西

答案 6 :(得分:0)

string.split("\t")这样的标签上拆分每一行,并使用字符串类的isEmpty()方法检查每个标记。

相关问题