Java 缓冲区读取器读取带逗号的字符串,然后插入2d数组
我的proccesos.txt有这个Java 缓冲区读取器读取带逗号的字符串,然后插入2d数组,java,arrays,split,2d,bufferedreader,Java,Arrays,Split,2d,Bufferedreader,我的proccesos.txt有这个 1500600 2,300,800 3,800,1000 42005000 我在这里要做的是将procesos.txt插入一个2d数组中,该数组将显示为以下4行3列,但没有comamas 这是我当前在按钮上的代码 ` `int line=0; 而((sCurrentLine=br.readLine())!=null){ String[]tmp=sCurrentLine.split(,“”;//将行拆分为单独的值 对于(int i=0;i
1500600
2,300,800
3,800,1000
42005000
我在这里要做的是将procesos.txt插入一个2d数组中,该数组将显示为以下4行3列,但没有comamas
这是我当前在按钮上的代码
`
`int line=0;
而((sCurrentLine=br.readLine())!=null){
String[]tmp=sCurrentLine.split(,“”;//将行拆分为单独的值
对于(int i=0;i<3;i++)
myArr[line][i]=Integer.parseInt(tmp[i]);
//将值转换为整数并将其插入匹配位置
//阵列中
line++;
}
我想将其插入myArr[][]中,如果您能对您的答案进行一些解释,这将是一件好事,因为可能会有人不完全理解您的实现。java.lang.NullPointerException我在myArr[line][I]=Integer.parseInt(tmp[I])上遇到了一个此错误;你能评论stacktrace的完整第一行吗?我已经测试了代码。对我来说很好。可能是某个尚未初始化的变量?
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();
}
}
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++;
}