【问题标题】:Buffer Reader reading String with comma, Then inserting 2d array缓冲区阅读器用逗号读取字符串,然后插入二维数组
【发布时间】:2015-06-18 04:02:22
【问题描述】:

我的 proccesos.txt 有这个

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

我在这里尝试做的是在一个二维数组中插入 procesos.txt,该数组将显示为以下 4 行 3 列但没有逗号

这是我目前在按钮上的代码

`

 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();
        }
    }

`

【问题讨论】:

  • 我想插入到myArr[][]

标签: java arrays split 2d bufferedreader


【解决方案1】:
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++;
}

【讨论】:

  • 如果您可以对您的答案进行一些解释,那将是很好的,这对那些可能不完全理解您的实现的人来说是有益的。
  • java.lang.NullPointerException 我在 myArr[line][i] = Integer.parseInt(tmp[i]);
  • 你能评论堆栈跟踪的完整第一行吗?
  • 我已经测试了代码。对我来说很好。也许你还没有初始化一些变量?
猜你喜欢
  • 2015-11-19
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
相关资源
最近更新 更多