-1

I am trying to write a java program that reads a texfile line by line and store each line in its own array so that these lines will then become data columns. For example, the data.txt file below has the following data:

  • 122,80,100,119,162,90,136...
  • 122,80,100,119,162,90,136...
  • 64,74,70,64,76,62,84,78 ...
  • positive,negative,negative ...

I want each line to be stored in its own array, hence 4 lines, 4 different arrays since each line will belong to a specific column. I am able to read the textfile.

BufferedReader br = new BufferedReader(new FileReader("C://data.txt"));
        ArrayList lines = new ArrayList();
        for(String line = br.readLine();line != null;line = br.readLine()) {
            line.replaceAll(",","\\.");
            String[] fields = line.split(" ");
            System.out.println(" " + fields[0]);
            lines.add(fields);
        }
        String[][] strings = (String[][]) lines.toArray(new String[lines.size()][]);
        System.out.println("Total Lines: " + strings.length);

Here's an expected output:

output

user3225573
  • 57
  • 1
  • 1
  • 6
  • You can use List of List (not arrays), it's easier. – Tokazio Sep 26 '16 at 11:26
  • 1
    `line` is already a line, you put this `line` in a collection say `ArrayList`, you can then iterate over that collection – Murtaza Khursheed Hussain Sep 26 '16 at 11:26
  • 2
    What do you mean "merged together"? Where do you see it merged together? I see a array of arrays being created. What do you expect to happen, and what happens instead? – RealSkeptic Sep 26 '16 at 11:31
  • 1
    Please [edit] your question and clarify your problem description. BTW `line.replaceAll(",","\\.");` doesn't do anything in your code, except creating string which you are immediately discarding (since strings are immutable so you can't edit them, but you can assign new string instance to string variable). Also there is no need to escape `.` in replacement part of `replaceAll`, Actually since you don't even want to use regular expressions you don't need `replaceAll` at all, use `replace` instead. – Pshemo Sep 26 '16 at 11:39
  • Also I don't see any spaces in your `data.txt` file so `split(" ")` doesn't really make sense. So for now `line.replaceAll(",","\\.");` problem makes your question duplicate of ["String not replacing characters"](http://stackoverflow.com/questions/12734721/string-not-replacing-characters) and splitting on space looks like typo problem (which is off-topic on Stack Overflow). – Pshemo Sep 26 '16 at 11:47

2 Answers2

0

If you want each line in a specific array then the following should help

BufferedReader br = new BufferedReader(new FileReader("C:\\data.txt"));
ArrayList lines = new ArrayList();
for(String line = br.readLine();line != null;line = br.readLine()) {
    //comma will be replaced with .
    line=line.replaceAll(",","\\.");
    //each line will be stored in an array with . as separator
    String[] fields = line.split("\\.");
    //System.out.println(" " + fields[0]);
    lines.add(fields);
}
String[][] strings = (String[][]) lines.toArray(new String[lines.size()][]);
System.out.println("Total Lines: " + strings.length);

//code to display
for(String s[]:strings){
    for(String ss:s){
        System.out.print(ss+", ");
    }System.out.println();
}

Its Output

Total Lines: 4
122, 80, 100, 119, 162, 90, 136, 
122, 80, 100, 119, 162, 90, 136, 
64, 74, 70, 64, 76, 62, 84, 78 , 
positive, negative, negative , 
Loki
  • 801
  • 5
  • 13
0

I think it's what you need:

        BufferedReader br = new BufferedReader(new FileReader("C://data.txt"));
        ArrayList<String[]> lines = new ArrayList<String[]>();
        for(String line = br.readLine();line != null;line = br.readLine()) {
            String[] fields = line.split(",");
            lines.add(fields);
        }
        System.out.println("Total Lines: " + lines.size());
        for(String[] lns:lines) {
            for (int i = 0; i < lns.length; i++) {
                if (i == lns.length - 1) System.out.print(lns[i] + "\n"); else System.out.print(lns[i] + ", ");
            }
        }
siarheib
  • 159
  • 1
  • 8