0

After I import the data in java using buffered reader from a .dat file I need to split it from the following format

2625::2120::2::973635271

to

4 arrays containing each one of them

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class Import {



        public static void main(String[] args) throws IOException {
        String fileName = "C:/Users/Sharad/Desktop/ml-1m/ratings.dat";


        readUsingBufferedReader(fileName);
    }
    private static void readUsingBufferedReader(String fileName) throws     IOException {
        File file = new File(fileName);
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;
        while((line = br.readLine()) != null){
            //process the line
            System.out.println(line);
        }
        //close resources
        br.close();
        fr.close();
    }
}

this is the code I am using to get this output from a .dat file

2625::2120::2::973635271

now I want to split each number to a different array.

Andrea
  • 11,801
  • 17
  • 65
  • 72
  • Why do you want something like this? Putting all number in one array is better than putting them in different array. – dijkstra Nov 23 '15 at 07:59
  • Possible duplicate of [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Aniruddha Varshney Jan 31 '17 at 09:57

1 Answers1

0
String[] data = line.split("::");
String[][] arrays = new String[data.length()][1];
for(int i=0; i<data.length(); i++){
      arrays[i][0] = data[i];
}

//example usage
String[] firstElementArray = arrays[0];
String[] secondElementArray = arrays[1];
...
dijkstra
  • 1,068
  • 2
  • 16
  • 39