Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

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

public class Main {
    public static double[] readVectorDouble(String path) throws FileNotFoundException {
        ArrayList<Double> arr = new ArrayList<Double>();
        System.err.println("Load vector from " + path);
        Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
        while (sc.hasNext()) {
            String line = sc.nextLine();
            if (line.isEmpty()) {
                break;
            }
            if (line.startsWith("%%")) { // detect matrix market format
                System.err.println(line);
                while (sc.nextLine().startsWith("%"))
                    ; // skip the comment line, and the dimension info.
                continue;
            }
            arr.add(Double.parseDouble(line));
        }
        System.err.println("Length: " + arr.size());
        double[] ret = new double[arr.size()];
        for (int i = 0; i < arr.size(); i++)
            ret[i] = arr.get(i);
        System.err.println("Done.");
        return ret;
    }
}