Java Scanner Read loadTimeseries(File file, String delimeter, int _amountOfLines)

Here you can find the source of loadTimeseries(File file, String delimeter, int _amountOfLines)

Description

Load a 3D (3 axes) time series csv file to a 2D array.

License

Open Source License

Parameter

Parameter Description
file a parameter
delimeter a parameter
_amountOfLines a parameter

Exception

Parameter Description
FileNotFoundException an exception

Declaration

public static double[][] loadTimeseries(File file, String delimeter,
        int _amountOfLines) throws FileNotFoundException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileNotFoundException;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    /**//from www  .  jav  a2 s .  c o  m
     * Load a 3D (3 axes) time series csv file to a 2D array.
     * 
     * @param file
     * @param delimeter
     * @param _amountOfLines
     * @return
     * @throws FileNotFoundException
     */
    public static double[][] loadTimeseries(File file, String delimeter,
            int _amountOfLines) throws FileNotFoundException {
        // load values to list
        Scanner scanner = new Scanner(file);
        scanner.useDelimiter("\n");
        List<Double[]> valuesList = new ArrayList<Double[]>();
        while (scanner.hasNext()) {
            String[] tokens = scanner.next().split(delimeter);
            Double[] curVal = new Double[3]; // we expect 3 orthogonal axes
            for (int i = 0; i < curVal.length; i++) {
                curVal[i] = Double.valueOf(tokens[i]);
            }
            valuesList.add(curVal);
        }
        scanner.close();
        // convert to 2D array
        if (_amountOfLines == -1) {
            _amountOfLines = valuesList.size();
        }
        double[][] valuesArray = new double[_amountOfLines][3];
        for (int sampleNr = 0; sampleNr < _amountOfLines; sampleNr++) {
            Double[] curVal = valuesList.get(sampleNr);
            for (int i = 0; i < curVal.length; i++) {
                valuesArray[sampleNr][i] = curVal[i];
            }
        }
        return valuesArray;
    }
}

Related

  1. loadNDimDoubleMatrix(Scanner reader, int numberOfDim, Object nmatrix)
  2. loadNDimDoubleMatrixRec(Scanner reader, int[] dim, Object nmatrix)
  3. loadSentencesMap(String path)
  4. loadSolution(File file)
  5. loadTestProgram(final Class resourceClass, final String fileName)
  6. read()
  7. read(Class clazz, String f)
  8. readArr(Scanner in, int n)
  9. readCQLFile(String cqlFileName)