简体   繁体   中英

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

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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