Parse comma-separated list of ints and return as array : CSV File « Development Class « Java






Parse comma-separated list of ints and return as array

    
import java.util.StringTokenizer;

/* Copyright (c) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.google.gdata.util.common.base;


/**
 * Some common string manipulation utilities.
 */
public class Util{
    /** Parse comma-separated list of ints and return as array. */
    public static int[] splitInts(String str) throws IllegalArgumentException {
      StringTokenizer tokenizer = new StringTokenizer(str, ",");
      int n = tokenizer.countTokens();
      int[] list = new int[n];
      for (int i = 0; i < n; i++) {
        String token = tokenizer.nextToken();
        list[i] = Integer.parseInt(token);
      }
      return list;
    }


}

   
    
    
    
  








Related examples in the same category

1.A utility class that parses a Comma Separated Values (CSV) file
2.Simple demo of CSV parser classSimple demo of CSV parser class
3.CSV in action: lines from a file and printCSV in action: lines from a file and print
4.Simple demo of CSV matching using Regular Expressions
5.Helper class to write table data to a csv-file (comma separated values).
6.Builds a bracketed CSV list from the array
7.Builds a CSV list from the specified String[], separator string and quote string
8.Builds a CSV list from the specified two dimensional String[][], separator string and quote string.
9.The csv tokenizer class allows an application to break a Comma Separated Value format into tokens.
10.The CSVQuoter is a helper class to encode a string for the CSV file format.
11.A stream based parser for parsing delimited text data from a file or a stream
12.Reads CSV (Comma Separated Value) files
13.Writes CSV (Comma Separated Value) files
14.Csv Converter
15.CVS reader
16.CSV Writer
17.CSV parser
18.Csv Reader
19.A very simple CSV parser released under a commercial-friendly license.
20.A very simple CSV reader released under a commercial-friendly license.
21.A very simple CSV writer released under a commercial-friendly license.
22.CSV file reader
23.CSV file writer
24.CSV Tokenizer Util
25.Parse a line of text in CSV format and returns array of Strings Implementation of parsing is extracted from open-csv.
26.CSV Writer
27.Parse comma-separated list of longs and return as array