简体   繁体   English

在检查数据类型的同时解析 Java 中的数据

[英]Parsing data in Java while checking data types

I primarily code in Python and I am completely new to java, so I am having difficulty with a simple programming task in Java regarding parsing through a .csv file.我主要使用 Python 编写代码,而且我对 Java 完全陌生,因此我在使用 Java 进行有关解析 .csv 文件的简单编程任务时遇到了困难。 My .csv file has multiple columns and I want to parse through each line and store the second column as a string and the last column (column 4) as a double as a (string, double) pair.我的 .csv 文件有多个列,我想解析每一行并将第二列存储为字符串,将最后一列(第 4 列)存储为双精度(字符串,双精度)对。 However, if column four does not contain a value that can be cast as a double value, I would like to assign a 0.0 as the double in the pair for that line.但是,如果第四列不包含可以转换为双精度值的值,我想分配一个 0.0 作为该行的双精度值。 Each line from the .csv is passed to this function below, and I attempt to store the (string, double) pairs as mentioned, but after executing, all the pairs have 0.0 as the double value. .csv 中的每一行都传递给下面的这个函数,我尝试存储提到的 (string, double) 对,但在执行后,所有对的双精度值都是 0.0。 I am not sure if there is there is a problem in my try/catch or looping method through each token.我不确定我的 try/catch 或每个令牌的循环方法是否存在问题。 Any hints are appreciated.任何提示表示赞赏。

        public void a(Text t) {
            StringTokenizer word = new StringTokenizer(t.toString(), ", ");
            int count = 0;
            double val = 0.0;
            String keep = new String("");
            boolean loop = true;
            while (loop) {
                String nextWord = word.nextToken ();
                if (count == 2) {
                    //string in pair
                    keep = nextWord;
                    
                    //loop until at last column and store word
                    while (word.hasMoreTokens()){
                        nextWord = word.nextToken();
                    }
                    loop = false;
                    
                    //check if string can be cast to double
                    try{
                        Double.parseDouble(nextWord);
                    } catch(NumberFormatException e) {
                        val = 0.0;
                    } catch(NullPointerException e) {
                        val = 0.0;
                    }
                    val = Double.parseDouble(nextWord);
                }
                count++;
            }
            // then not relevant code to store (keep, val) pair for rest of code
        }

You should avoid StringTokenizer because it is a deprecated library.您应该避免使用StringTokenizer因为它是一个已弃用的库。 Using string.split() .使用string.split() Here is a much simpler solution这是一个更简单的解决方案

public void a(Text t) {
   String[] line = t.toString().split(", ");
   //check if string can be cast to double
   try{
      Double.parseDouble(line[3]);
   } catch(NumberFormatException e) {
      line[3] = "0.0";
   }
}

If the column 4 can be casted to double, it will keep it as it is otherwise it will put it as "0.0".如果第 4 列可以转换为双倍,它将保持原样,否则将其设为“0.0”。 The caveat is that since java can only have one datatype in string, you can't store it as double, however, whenever you want to use this value, you can parse it on spot without worrying that it will throw an exception".需要注意的是,由于 java 字符串中只能有一种数据类型,因此您不能将其存储为双精度值,但是,无论何时您想使用此值,您都可以当场解析它,而不必担心它会引发异常”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM