检查

2013-05-02 46 views
1

我读这是制表符分隔如下图所示检查

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

,我能够成功地读取它(在表中的所有列的文本文件ABC .TXT为null或空白字符串我已经开发了一个单独的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) 
{ 
} 

这个方法我在这里取一个字符串中的完整的行,并且在这个方法里面我想把这个字符串中的标记分开,然后进一步评估那些标记为空或者空,如果有的话我会返回false

+0

你如何阅读文件? – NINCOMPOOP 2013-05-02 06:34:59

+0

更新了帖子 – 2013-05-02 06:37:57

+0

@ user2200150当至少有一个字段为空时,您已经引发异常。你确切的问题是什么?你能展示一些代码来理解你真正的问题吗? – 2013-05-02 06:38:57

回答

0

您可以设置像干将

公共字符串gethgh)空校验( {

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

}

这里UserDefinedException必须b Ë声明为类

我认为这应该工作

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

这将是C#... – 2013-05-02 09:39:48

+0

@WhiletrueSleep我不能在Java 5字符串类 – 2013-05-02 09:42:52

+0

@ user2200150编辑我的帖子没有看到它是Java – WhileTrueSleep 2013-05-02 09:44:38

0

让我们假设你有一个方法getNextValueAndPosPair()返回下一您的tsv(标签单独值)文件的值以及读取值后发现的最后一个空格的位置(ig a 标签或a Newline)。

然后,您可以通过切线验证您的输入行,它包含由选项卡分隔的正好三个不同的值。

下面的实现:

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

看看阿帕奇串utils的库。它可能有你需要的东西

0

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