0

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
        }
7
  • If you use string.split(",") it will be better and faster. StringTokenizer is a depricated library. It will give you an array of string and performing operations on it will be easy as well. Commented Oct 12, 2020 at 16:34
  • 1
    I would strongly encourage you to use an existing CSV parsing library rather than build your own. CSV has some interesting edge cases that a good library will handle for you. Commented Oct 12, 2020 at 16:34
  • 1
    This line looks problematic: val = Double.parseDouble(nextWord); Did you mean to do that parsing and assignment inside the try block? Commented Oct 12, 2020 at 16:37
  • 1
    @JBL It should be in the try block because right now it will run regardless of whether there is an error or not. Commented Oct 12, 2020 at 16:42
  • 1
    The problem with the Internet is that you can find sample code that was written twenty years ago and have no clue that it was written twenty years ago and that it is no longer appropriate. From the javadoc of class StringTokenizer: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. Commented Oct 12, 2020 at 16:54

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.