Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q4009 How do I parse a comma delimited string?

You are here: irt.org | FAQ | Java | Q4009 [ previous next ]

Use the StringTokenizer object, the following is a snippet of code which fills in 4 values (Open, High, Low and Close) in respective variables from an string ohlc which is passed as a parameter to the method

public Candle(String ohlc){
      /**
       * The StringTokenizer class lets us separate ohlc
       * using custom separators. I used a space, a colon, a comma,
       * and a semicolon as separators.
       */
      StringTokenizer st=new StringTokenizer(ohlc," :,;");
      open=(new Double(st.nextToken())).doubleValue();
      high=(new Double(st.nextToken())).doubleValue();
      low=(new Double(st.nextToken())).doubleValue();
      close=(new Double(st.nextToken())).doubleValue();
      
   }

©2018 Martin Webb