檢查

2013-05-02 158 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()字符串類的方法檢查每個標記。