Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查字符串是否为null或空白_Java_String_Io - Fatal编程技术网

Java 检查字符串是否为null或空白

Java 检查字符串是否为null或空白,java,string,io,Java,String,Io,我正在读取一个文本文件abc.txt,该文件以制表符分隔,如下所示 gfh hgh thf --- --- --- fgh sji irj rhf dhh fhf kji idj ddt String n = f.getgfh()+f.gethgh()+f.getthf(); //it will contain the contents of the row 1 我能够成功地读取它(对于表中的所有列,我已经开发了一个单独的pojo以及getter和setter),

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

gfh  hgh  thf
---  ---  ---
fgh  sji  irj   
rhf  dhh  fhf
kji  idj  ddt
String n = f.getgfh()+f.gethgh()+f.getthf();  //it will contain the contents of the row 1
我能够成功地读取它(对于表中的所有列,我已经开发了一个单独的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 
现在我所遵循的方法是在字符串中按行获取值,如下所示

gfh  hgh  thf
---  ---  ---
fgh  sji  irj   
rhf  dhh  fhf
kji  idj  ddt
String n = f.getgfh()+f.gethgh()+f.getthf();  //it will contain the contents of the row 1
创建一个单独的方法并将传递此字符串

private boolean validaterow(String f)
{
}

在这个方法中,我在一个字符串中获取完整的行,在这个方法中,我想在标记中打断这个字符串,然后进一步评估这些标记是否为空或null,如果有,我将返回false,您可以像在getters中一样设置null检查

公共字符串gethgh() {

}

此处必须将UserDefinedException声明为类


<>我认为这应该起作用

为什么把它传递给一个方法?

使用

您可以在连接时检查您的条件

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;

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

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

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

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

然后,您可以通过检查输入行是否包含由选项卡分隔的三个不同值来验证输入行

执行情况如下:

// 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;
}
//一个简单的通用pair类。
类对{
配对(第一对,第二对){
this.first=first;
这个秒=秒;
}
public A first(){return first;}
public A second(){return second;}
私人决赛第一名;
私人决赛B秒;
}
//如果此行包含4个不同的值,则返回true,否则返回false。
私有布尔validaterow(字符串f){
int pos=0;
对于(int i=0;i<3;i++){
Pair valueAndPos=getNextValueAndPosPair(f,pos);
字符串值=value和pos.first();
int pos=value和pos.second();
if(value.isEmpty()){
System.err.println(“第“+i+”-th个值为null”);
返回false;
}
pos+=1;
}
返回true;
}
//返回一对,其中包含行中的下一个tsv值和分隔符空间的位置
私有对getNextValue和posPair(字符串f,int start){
整数长度=0;
对于(int i=start;i
试试这个

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

看看apachestringutils库。它可能具有您所需的内容

在选项卡上拆分每一行,如
string.Split(“\t”)
,并使用string类的
isEmpty()
方法检查每个标记。

您如何读取该文件?@user2200150当至少一个字段为
null
时,您已经抛出异常。你到底有什么问题?你能展示一些代码来理解你真正的问题吗?@LuiggiMendoza我认为这是正确的方法@user2200150根据当前对问题的描述,没有正确的方法,因为您可以选择不同的设计。如果您只需要在至少一个字段为
null
时抛出异常,那么您已经拥有了它。您还可以检查字段是否为null,并为每个字段抛出一个异常,其中包含更详细的消息。另一种选择是使用反射。再说一遍:这个问题没有确切的答案,这取决于你的需要。你真的应该使用一个框架来解析这样的文件。@WhiletrueSleep我不能在java 5字符串类中找到它