read Double Array from InputStream - Java java.lang

Java examples for java.lang:Math Array Function

Description

read Double Array from InputStream

Demo Code


//package com.java2s;

import java.io.*;
import java.util.*;

public class Main {
    public static double[] readDoubleArray(InputStream stream)
            throws IOException {
        List<Double> listRep = new ArrayList<Double>();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                stream));//from w  ww  .  ja v  a2s  . co  m
        while (reader.ready()) {
            String line = reader.readLine();
            String[] vals = line.trim().split("\\s+");
            for (String val : vals) {
                listRep.add(Double.parseDouble(val));
            }
        }
        reader.close();

        int size = listRep.size();
        double[] arrayRep = new double[size];

        for (int i = 0; i < size; i++) {
            arrayRep[i] = listRep.get(i);
        }

        return arrayRep;
    }
}

Related Tutorials