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

huangapple go评论86阅读模式
英文:

Parsing data in Java while checking data types

问题

我主要使用Python进行编程,对于Java我完全是新手,所以在Java中有一个简单的编程任务让我感到困难,涉及解析 .csv 文件。我的 .csv 文件有多列,我想逐行解析并将第二列存储为字符串,将最后一列(第四列)存储为双精度数值,形成一个(字符串,双精度)的对。然而,如果第四列不包含可以转换为双精度值的内容,我希望将该行中的双精度值设为 0.0。每一行从 .csv 文件传递到下面的这个函数中,我尝试按照上述要求存储(字符串,双精度)对,但在执行后,所有的对中双精度值都是 0.0。我不确定是在 try/catch 部分还是在遍历每个标记时存在问题。如有任何提示,将不胜感激。

        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++;
            }
            // 然后是存储(keep,val)对的其余代码
        }
英文:

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. 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. 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. 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. I am not sure if there is there is a problem in my try/catch or looping method through each token. 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
        }

答案1

得分: 1

你应避免使用StringTokenizer,因为它是一个已弃用的库。应使用string.split()。这里是一个更简单的解决方案:

public void a(Text t) {
   String[] line = t.toString().split(", ");
   //检查字符串是否可以转换为double
   try{
      Double.parseDouble(line[3]);
   } catch(NumberFormatException e) {
      line[3] = "0.0";
   }
}

如果第四列可以转换为double,它将保持不变,否则将其设置为"0.0"。需要注意的是,由于Java中字符串只能有一种数据类型,你无法将其存储为double,但是每当你想要使用这个值时,可以立即解析它,而不必担心会抛出异常。

英文:

You should avoid StringTokenizer because it is a deprecated library. Using 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". 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".

huangapple
  • 本文由 发表于 2020年10月13日 00:27:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64321683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定