0

I am trying to use Java to read the parameters for a problem instance from an input file. for example, suppose a problem is defined by the three parameters seasonality, prices and elasticity, each of which are arrays. Is there a standard file format for input files? It could look like the one below, or it could have another standard format.

seasonality
10 11 12 13 14 15
prices
10 9 8 7
elasticity
-4 2 1

Is there a standard library or parser which will automatically parse such input files?

Clarification in response to comments

For my purposes, it would be perfect if the Reader class returns a List mapping each parameter to the parameter value which can be either an array or a scalar. A second class can read the Reader class to create a Problem instance using the parameters defined in the Reader class.

2
  • Do you already know beforehand the names and number of the parameter groups, or can they vary from run to run? There isn't one specific "the standard", but reasonable options include space-separated numbers for simple inputs (use java.util.Scanner and a List to store them in), or JSON if you don't know beforehand how many collections of parameters you'll need. Commented Dec 22, 2013 at 23:31
  • csv (comma separated values) could be feasible format for you. Look at stackoverflow.com/questions/12410538/… Commented Dec 22, 2013 at 23:32

2 Answers 2

1

For this problem, I suggest using a Map<String,List<Integer> and the sample format you showed. Open a Scanner on your input file, read a String to be the key, and then read ints until you run out. Repeat until EOF.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. See implementation in my answer below.
1

Based on chrylis answer, I created a class that parses such files with each parameter and value separated by whitespace.

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * Read an input file which has multiple parameters, where the
 * name of each is a string not equal to a number, and is followed
 * by the a scalar or array of integers or doubles.
 * Each parameter and number is separated by white space.
 */
public class NumberParser {

    public static Map<String, Object> parse(String file) throws Exception {
        Scanner scanner = new Scanner(new File(file));

        Map<String, Object> map = new HashMap<String, Object>();

        while (scanner.hasNext()) {
            String parameter = scanner.next();

            if (scanner.hasNextInt()) {
                List<Integer> list = new LinkedList<Integer>();
                while (scanner.hasNextInt()) {
                    list.add(scanner.nextInt());
                }
                map.put(parameter, listToIntArray(list));
            } else { // double array
                List<Double> list = new LinkedList<Double>();
                while (scanner.hasNextDouble()) {
                    list.add(scanner.nextDouble());
                }
                map.put(parameter, listToDoubleArray(list));
            }
        }

        scanner.close();

        return map;
    }

    private static int[] listToIntArray(List<Integer> list) {
        int[] array = new int[list.size()];
        int i = 0;
        for (Iterator<Integer> itr = list.iterator(); itr.hasNext(); ++i) {
            array[i] = itr.next();
        }
        return array;
    }

    private static double[] listToDoubleArray(List<Double> list) {
        double[] array = new double[list.size()];
        int i = 0;
        for (Iterator<Double> itr = list.iterator(); itr.hasNext(); ++i) {
            array[i] = itr.next();
        }
        return array;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.