4

我是一个 java 菜鸟,也是这个网站的新手,所以请在这里多多包涵。如果我在发布此内容时做错了什么,请告诉我,对于我所做的任何事情或任何错误的语法,我提前道歉。

我需要在 java 中编写一个自定义 CSV 解析器,它不会在引号内分隔逗号。我不能使用任何与 CSV 类相关的东西。1,2,3,4 -> 1 2 3 4 a,"b,c",d -> ab,cd

无论我尝试什么,我总是会遇到异常

import java.io.*;
import java.util.*;

public class csv
{
    public static void main(String[] args)
    {
        File file = new File("csv.test");
        BufferedReader buf = null;

        try
        {
            buf = new BufferedReader(new FileReader(file));

            String str;

            while ((str = buf.readLine()) != null)
            {
                String[] values = null; // array for saving each split substr
                int c1 = 0; // counter for current position in string
                int c2 = 0; // counter for next space in array to save substr
                int c3 = 0; // location of last comma or quotation for substring creation
                int i = 0; // counter used later for printing

                while (c1 <= str.length())
                {
                    char x = str.charAt(c1);
                    String s = Character.toString(x);

                    if (c1 == str.length())
                    {
                        values[c2] = str.substring(c3, (c1 - 1));
                    }
                    else if (s == ",")
                    {
                        values[c2] = str.substring(c3, (c1 - 1));
                        c1++;
                        c2++;
                        c3++;
                    }
                    else if (s == "\"")
                    {
                        c1++;
                        c3 = c1;
                        while (s != "\"")
                            c1++;
                        values[c2] = str.substring(c3, (c1 - 1));
                        c1 = c1 + 2;
                        c2++;
                        c3 = c1;
                    }
                    else
                    {
                        c1++;
                    }
                    while (i < values.length)
                        System.out.println("\"" + values[i] + "\"");
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("Error locating test file");
        }
    }
}

我尝试删除所有逻辑并测试它是否与文件 io 相关。读起来很好,所以我归结为它与逻辑有关。我看了一遍,找不到任何问题。我什至让一个朋友看过它,说它看起来不错。异常在哪里抛出?

4

3 回答 3

3

你没有values在这一行初始化你,String[] values = null;因此当你使用它时它会失败,即在 list values[c2] = str.substring(c3, (c1 - 1));

要解决此问题,请values使用适当的长度初始化数组,例如String[] values = new String[SIZE];. 可能您需要使用str.length()as SIZEof 数组。

同样在您的比较else if (s == ",")中,您正在比较 String using ==,这是不正确的。相反,您可以将x自身用作else if (x == ',').

编辑:您的以下条件将c1无限增加,因为您没有更改str(x after correction as advised)任何地方的值。

旧状态:

             while (s != "\"")
                  c1++;

按照建议进行更改后(仍然错误,因为 x 在 while 循环中没有更改):

             while (x != '"')
                  c1++;

请更正循环逻辑。

于 2012-11-19T03:14:11.347 回答
1

与具体问题无关,但s == ","您应该这样做",".equals(s),与其他字符串相等检查类似。

于 2012-11-19T03:15:38.600 回答
0

这不是您的异常的原因,但它显然是不正确的:

    while (s != "\"")
        c1++;

这是不正确的,原因有两个:

  • 由于c1++不会改变循环条件,这将永远循环。
  • 当您应该使用==/来比较字符串时,您正在使用. 您似乎在其他地方也犯了这个错误。!=equals(...)

要找出导致异常的原因,您应该做的第一件事是打印出堆栈跟踪。添加e.printStackTrace();到 catch 块。

或者更好的是,将其更改为 catch IOException。捕捉Exception是个坏主意……除非您要记录/输出完整的堆栈跟踪。

于 2012-11-19T03:19:34.177 回答