Pregunta

I have searched through the site all morning and have not found an answer to this that works for me. If there is another answer please excuse me. This might be an easy question but for some reason it has stumped me at work. I have a .csv file (there is not restriction though and it could be a .txt file or anything I want it to be) right now there are only two values, Amplitude and Frequency (later there will be more). I write a class to import one column into an array thinking that I could get it to do the second easily. Hasn't been so easy. Here is the code for the single array. Any help on how to get it to a multidimensional array would be appreciated.

public static ArrayList<Integer> readData(String filename) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File(filename));
    ArrayList<Integer> aList = new ArrayList<>();
    while(scanner.hasNextInt()) {
        aList.add(scanner.nextInt());         
        }
    return aList;
}

And here is my main class. Basically I need to take whatever values and store them in a multidimensional array for the time being. They will then be passed to the controller via MODBUS and a holding register.

public static void main(String[] args) throws FileNotFoundException, Exception {

    ArrayList<Integer> aList = BrushSealDataFactory.readData("AmplitudeFrequency.csv");
    for (int i = 0; i < aList.size(); i++) {
        System.out.println(aList.get(i));
    }

    int[] aArray = new int[aList.size()];
    for (int i = 0; i < aList.size(); i++) {
        aArray[i] = aList.get(i);
    }     
¿Fue útil?

Solución

This is a good example of splitting a file into a 2-d array with java:

CSV data to 2d array java

Just be careful to cast the values correctly into whatever format you need.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top