Read an input text file and store the tokens in 2 different arrays I am very new to Java so please ignore if there are obvious mistakes. If my question seems redundant then please guide me towards the correct link. However, I have surfed enough in order to find the answer. Any changes in code will be greatly appreciated.

I am reading an input file and storing the elements of it in a 2D array. What I want to do is split that array in 2 separate arrays. 1st array would contain all the characters which is firstDimension in my code. Now, I want another array which stores all the integers in an array. I just have to print those arrays. This array should be created as soon as the special character &gt; is observed.

This can be done in 2 ways:


Read the strings in the file and store all of the elements in a 2D array and then divide the array into 1 double and one 2D char array
Read only chars from the file and store it in char array and then read only double values from the file and store it in different array.


My input file has text:


a A b u&gt;0.0001
b b X g&gt;0.0005
Y z N H&gt;0.0003



import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Delimiter {
    public static void main(String[] args)
    {
        try {
            Scanner scanner = new Scanner(new File("hello.txt"));
            scanner.useDelimiter(System.getProperty("line.separator"));
            ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
                while (scanner.hasNext()) {
                list.add(scanner.next());
            }
            scanner.close();
                // finally convert the arraylist to a char[][]
            char[][] firstDimension = new char[list.size()][];
            for (int i = 0; i &lt; list.size(); i++) {
                firstDimension[i] = list.get(i).toCharArray();
            }
                for (int i = 0; i &lt; firstDimension.length; i++)
            {
                for (int j = 0; j &lt; firstDimension[i].length; j++)
                {
                    //System.out.println(firstDimension[i][j]);
                    System.out.print(" "+firstDimension[i][j]);
                    System.out.println("");
                }
            }
            } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
A useful first step for you to take is to separate out 1 item of functionality in to a class. Your method should be broken up in to several classes, but you will get the idea with just one.

This one class, call it DataParser for want of a better name, will be used like this:

DataParser parser = new DataParser();
// set up a loop over the input data
for (String line : inputlines) {
    parser.parseLine(line)
}
char[][] chars = parser.getChars();
double[] doubles = parser.getDoubles();


The advantage of this is that you can separate out your file-reading logic from your parsing logic.

Now, about how we read the file. I recommend something simple that's new in Java 7: Files.readAllLines(Path, Charset).

Path inputpath = Paths.get("hello.txt");
List&lt;String&gt; inputlines = Files.readAllLines(inputpath, StandardCharsets.UTF_8);


OK, we have our input lines that way, and we can string together the two sections:

Path inputpath = Paths.get("hello.txt");
List&lt;String&gt; inputlines = Files.readAllLines(inputpath, StandardCharsets.UTF_8);

DataParser parser = new DataParser();
// set up a loop over the input data
for (String line : inputlines) {
    parser.parseLine(line);
}
char[][] chars = parser.getChars();
double[] doubles = parser.getDoubles();


Now, about that DataParser class.... it should look something like:

public class DataParser {
    private final List&lt;char[]&gt; charlist = new ArrayList&lt;&gt;();
    private final List&lt;Double&gt; doublelist = new ArrayList&lt;&gt;();

    public void parseLine(String line) {
        int charpos = line.indexOf('&gt;');
        if (charpos &gt;= 0) {
            charlist.add(line.substring(0, charpos).toCharArray());
            doublelist.add(Double.parseDouble(line.substring(charpos + 1)));
        } else {
            // the line does not have a &gt;
            // throw an exception?
        }
    }

    public char[][] getChars() {
        return charlist.toArray(new char[charlist.size()][]);
    }

    public double[] getDoubles() {
        double[] ret = new double[doublelist.size()];
        int cnt = 0;
        for (Double d : doublelist) {
            ret[cnt++] = d;
        }
        return ret;
    }

}


This class will 'accumulate' the parsed data, and return it when asked. It needs to do some tricks with both accumulators to get the data out in the right format, but you should be able to figure it out.

Edit Here, I did it for you:

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class DataParser {
    private final List&lt;char[]&gt; charlist = new ArrayList&lt;&gt;();
    private final List&lt;Double&gt; doublelist = new ArrayList&lt;&gt;();

    public void parseLine(String line) {
        int charpos = line.indexOf('&gt;');
        if (charpos &gt;= 0) {
            charlist.add(line.substring(0, charpos).toCharArray());
            doublelist.add(Double.parseDouble(line.substring(charpos + 1)));
        } else {
            // the line does not have a &gt;
            // throw an exception?
        }
    }

    public char[][] getChars() {
        return charlist.toArray(new char[charlist.size()][]);
    }

    public double[] getDoubles() {
        double[] ret = new double[doublelist.size()];
        int cnt = 0;
        for (Double d : doublelist) {
            ret[cnt++] = d;
        }
        return ret;
    }

    public static void main(String[] args) throws IOException {
        Path inputpath = Paths.get("hello.txt");
        List&lt;String&gt; inputlines = Files.readAllLines(inputpath, StandardCharsets.UTF_8);

        DataParser parser = new DataParser();
        // set up a loop over the input data
        for (String line : inputlines) {
            parser.parseLine(line);
        }
        char[][] chars = parser.getChars();
        double[] doubles = parser.getDoubles();

        System.out.println(Arrays.deepToString(chars));
        System.out.println(Arrays.toString(doubles));
    }

}


Output:


   [[a,  , A,  , b,  , u], [b,  , b,  , X,  , g], [Y,  , z,  , N,  , H]]
   [1.0E-4, 5.0E-4, 3.0E-4]