1

我正在阅读一个以制表符分隔的文本文件 abc .txt,如下所示

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

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

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

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

4

7 回答 7

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;
    }
}
于 2013-05-02T09:37:54.317 回答
0

假设您有一个方法getNextValueAndPosPair()可以返回 tsv(制表符分隔值)文件的下一个值以及在读取的值之后找到的最后一个空格的位置(例如TabNewline)。

然后,您可以通过检查输入行是否包含由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;
}
于 2013-05-02T09:34:15.200 回答
0

查看 apache 字符串实用程序库。它可能有你需要的东西

于 2013-05-02T09:38:59.000 回答
0

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

公共字符串 gethgh() {

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

}

这里 UserDefinedException 必须声明为一个类

我认为这应该有效

于 2013-05-02T08:23:46.367 回答
0

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

于 2013-05-02T09:41:45.623 回答
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;
于 2013-05-02T09:30:17.867 回答
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;
}
于 2013-05-02T09:32:03.147 回答