【问题标题】:Checking the string for null or empty space检查字符串是否为空或空白
【发布时间】:2013-05-02 09:24:01
【问题描述】:

我正在读取一个由制表符分隔的文本文件 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

【问题讨论】:

  • 你读的怎么样?
  • @user2200150 当至少一个字段为null 时,您已经抛出异常。你的具体问题是什么?你能展示一些代码来理解你真正的问题吗?
  • @LuiggiMendoza 我认为这是正确的方法..!
  • @user2200150 从您的问题的当前描述来看,没有 正确的 方法,因为您可以选择不同的设计。如果您只需要在至少一个字段为null 时抛出异常,那么您已经拥有它。您还可以检查该字段是否为空并抛出异常,并为每个字段提供更详细的消息。另一种选择可以使用反射来完成。同样:对此没有确切的答案,这取决于您的需求。
  • 你真的应该使用像OpenCSV这样的框架来解析这些文件。

标签: java string io


【解决方案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;
    }
}

【讨论】:

    【解决方案2】:

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

    公共字符串 gethgh() {

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

    }

    这里 UserDefinedException 必须声明为一个类

    我认为这应该可行

    【讨论】:

      【解决方案3】:

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

      使用

      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;
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

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

            private boolean ValidateRow()
            {
               return !(StringIsNullOrEmpty((f.getgfh())) || 
                        StringIsNullOrEmpty((f.gethgh())) ||
                        StringIsNullOrEmpty((f.getthf()))
                        );
            }
            
            private bool StringIsNullOrEmtpy(string input)
            {
               return input.isEmpty() || input == null;
            }
            

            【讨论】:

            • @WhiletrueSleep 我无法在 java 5 字符串类中找到
            【解决方案7】:

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

            然后您可以通过检查输入行是否包含由 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;
            }
            

            【讨论】:

              猜你喜欢
              • 2015-06-05
              • 2017-08-19
              • 2015-06-12
              • 1970-01-01
              • 2010-10-23
              • 2020-05-29
              • 2017-01-10
              • 2014-07-27
              • 1970-01-01
              相关资源
              最近更新 更多