缓冲读取器用逗号读取字符串,然后插入2d数组

时间:2015-04-12 22:12:42

标签: java arrays split 2d bufferedreader

我的proccesos.txt有这个

1,500,600 2,300,800 3,800,1000 4,200,5000

我在这里尝试做的是将procesos.txt插入到一个2d数组中,该数组将显示为4行,包含3列但没有使用comamas

这是我在buttom上的代码

`

 try {

       String sCurrentLine;

        br = new BufferedReader(new FileReader("C:\\Users\\Ifrahim\\Desktop\\Procesos.txt"));
        br2 = new BufferedReader(new FileReader("C:\\Users\\Ifrahim\\Desktop\\Procesos.txt"));

        int lines = (int) br2.lines().count();
         myArr = new int[lines][3];

         String[] lineArray = null ;

        while ((sCurrentLine = br.readLine()) != null) {


            lineArray=sCurrentLine.split(",");

            System.out.println(Arrays.toString(lineArray));     
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

`

1 个答案:

答案 0 :(得分:0)

int line = 0;
while((sCurrentLine = br.readLine()) != null){
     String[] tmp = sCurrentLine.split(",");//split the line up into its separate values

     for(int i = 0 ; i < 3 ; i++)
          myArr[line][i] = Integer.parseInt(tmp[i]);
          //convert the values into integers and insert them at the matching position
          //in the array


     line++;
}