w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Turn text file into array

Try this :

Class

 Class DataClass
 {
   public int first; // Deliberately public 
   public int second;

   public DataClass(int val1, int val2){
   this.first = val1;
   this.second = val2;
   }
 }

Code

    public static void loadList() {

    ArrayList<DataClass> pairList = new ArrayList<DataClass>();
    BufferedReader br = new BufferedReader(new FileReader("./data.txt"));
    String line;
    while ((line = br.readLine()) != null) {
        String args[] = line.split(":");
        int first = Integer.valueOf(args[0]);
        int second = Integer.valueOf(args[1]);
        DataClass valPair = new DataClass(first,second);
        pairList.add(valPair);
        System.out.println(valPair.first + " : " + valPair.second);
    }
    br.close();
 }

Make sure you have proper try catch statements





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.