检查.csv单元格是否为空 [英] Check if a .csv cell is empty

查看:365
本文介绍了检查.csv单元格是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将.csv表添加到我的2d数组中,但想要整理出所有空单元格.

I am trying to add a .csv table into my 2d array, but want to sort out all the empty cells.

这是我的代码当前的外观:

This is how my code currently looks:

try {
        BufferedReader reader = new BufferedReader(new FileReader(upload.pathName));
        String strLine;

        for (int i = 0; (strLine = reader.readLine()) != null; i++) {
            Scanner scanner = new Scanner(strLine);
            scanner.useDelimiter(";");
            int j = 0;
            while (scanner.hasNext()) {
                try {
                    if (scanner.next() == null || scanner.next().equals("")) {
                        System.out.println("this cell is empty");
                        j++;
                    } else {
                     array[i][j] = scanner.next();
                     j++;
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
            scanner.close();
        }
        reader.close();
    } catch (Exception abcdefg) {
        //Exception
    }

我的.csv看起来像这样:

my .csv looks something like this:

value1;value2;value3;value5;value6;value7
value8;value9;value10;...

这条线还能用吗?:

if (scanner.next() == null || scanner.next().equals("")) 

有人知道为什么当我仅使用if(scanner.next()== null)或if(scanner.next().equals("))时j会循环直到其值为5并随后actiaves Exception abcdefg,但是当我使用if(scanner.next()== null || Scanner.next().equals(")))时,j只会循环直到其值为3,然后触发异常abcdefg

And does anybody know why when I use only if(scanner.next() == null) OR if(scanner.next().equals("")) j loops till it has the value 5 and afterwards actiaves Exception abcdefg, but when I use if(scanner.next() == null || scanner.next().equals("")) j only loops till it has the value 3 and then triggers the Exception abcdefg

我遗漏了什么/我的错误在哪里,为什么即使仍然存在不为空的单元格,它也会跳入异常?

What am I missing/where lies my mistake and why does it jump into the Exception, even when there are still cells that are not empty?

推荐答案

您的条件是否正常,除了您不想两次调用scanner.next(),这会吃掉"一些输入.

Your if condition is OK, except that you don't want to call scanner.next() twice, which will 'eat' some input.

您希望将scanner.next()的值设置为变量,然后在if条件下检查变量值:

You would want to set the value of scanner.next() to a variable and then check the variable value in the if condition:

String input = scanner.next(); 
if(input == null || input.equals("")){
    //Do something
}

这篇关于检查.csv单元格是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆