Java 填充2D数组的BufferReader缺少最后一行
我尝试使用BufferReader来填充2D数组,它成功地填充了除最后一行之外的所有数组行。我已经尝试将循环增加1来解释这一行,但是我得到了一个索引越界错误。如何才能显示最后一行Java 填充2D数组的BufferReader缺少最后一行,java,multidimensional-array,bufferedreader,Java,Multidimensional Array,Bufferedreader,我尝试使用BufferReader来填充2D数组,它成功地填充了除最后一行之外的所有数组行。我已经尝试将循环增加1来解释这一行,但是我得到了一个索引越界错误。如何才能显示最后一行 public static void inputArray(char[][] outArray, String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename));
public static void inputArray(char[][] outArray, String filename) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
for(int i = 0; i < outArray.length; i++) {
for(int j = 0; j < outArray[0].length; j++){
outArray[i][j] = (char)br.read();
}
}
br.close();
}
catch (IOException e) {
System.out.println("Error opening the file");
}
}
文件内容:
abcd
efgh
ijkl
mnop
qrst
uvwx
yzab
下面是我用来打印数组的代码:
for(int i = 0; i < test.length; i++) {
for(int j = 0; j < test[0].length; j++) {
System.out.printf("%1c",test[i][j]);
}
}
for(int i=0;i
您正在数组中存储换行符
考虑到该代码和该文件以及outArray
包含[7][4]个元素,outArray
应该最终包含:
outArray[0][0] == 'a'
outArray[0][1] == 'b'
outArray[0][2] == 'c'
outArray[0][3] == 'd'
outArray[1][0] == '\n'
outArray[1][1] == 'e'
outArray[1][2] == 'f'
outArray[1][3] == 'g'
outArray[2][0] == 'h'
outArray[2][1] == '\n'
outArray[2][2] == 'i'
outArray[2][3] == 'j'
// etc
outArray[6][0] == '\n'
outArray[6][1] == 'u'
outArray[6][2] == 'v'
outArray[6][3] == 'w'
或以表格形式:
0 1 2 3
0 'a' 'b' 'c' 'd'
1 '\n' 'e' 'f' 'g'
2 'h' '\n' 'i' 'j'
3 'k' 'l' '\n' 'm'
4 'n' 'o' 'p' '\n'
5 'q' 'r' 's' 't'
6 '\n' 'u' 'v' 'w'
这是因为您忽略了文件还包含换行符这一事实。如果您的文件不包含任何换行符(即,所有换行符都在一行上),那么您的代码将成功地将其读入7x4数组
如果您的文件始终具有相同的格式,则可以跳过换行符(因为您知道换行符的位置),如下所示:
for(int i = 0; i < outArray.length; i++) {
for(int j = 0; j < outArray[0].length; j++){
outArray[i][j] = (char)br.read();
}
br.read(); // read the next character (which will be a newline) and ignore it
}
for(int i=0;i
我知道伊梅比斯已经提供了一个很好的答案。这是我同时为你写的一些代码
我总是尽量避免假定文件的内容。这看起来可能有点令人望而生畏,但它使您能够专注于自己的意图(长度为4的数组),而不必担心需要满足多少需求。(如果文件内容添加了另一个字符,或删除了一个字符…或成为一本小说怎么办?)
String filename=“testData.txt”;
最终整数数组_SIZE=4;
BufferedReader br=null;
列表数据=新的ArrayList();
试一试{
br=新的BufferedReader(新的文件读取器(文件名));
int characterIndex=0;
char[]nextRow=null;
int charValue=0;
//您不必事先知道输入文件中有多少个字符
而((charValue=br.read())!=-1)
{
char c=(char)charValue;
//丢弃换行符
如果(c=='\n'| | c=='\r')
{
继续;
}
如果(characterIndex%数组大小==0)
{
nextRow=新字符[数组大小];
数据。添加(下一步);
CharacteristIndex=0;
}
nextRow[characterIndex++]=c;
}
br.close();
}
捕获(IOE异常){
System.out.println(“打开文件时出错”);
}
要遍历结果列表的一些代码:
int rowIndex = 0;
for(char[] row : data)
{
for(int i = 0; i < ARRAY_SIZE; i++)
{
System.out.print("array[" + rowIndex + "][" + i + "] - " + row[i]);
System.out.print("\n");
}
rowIndex++;
}
int行索引=0;
for(字符[]行:数据)
{
for(int i=0;i
祝你一切顺利
Luke发布文件内容,以便我们可以看到数组缺少了什么。outArray有多大?还有,你希望如何处理新行?outArray是[7][4],我不知道你说的处理新行是什么意思?@BenjiFrank你的文件包含
a
,然后b
,然后c
,然后d
,然后是新行,然后e
,然后f
,然后g
,然后h
,然后是新行,然后是i
,然后是j
,等等。那么,您是否尝试过使用调试器来找出问题?非常感谢。我没有意识到我正在存储'\n'
,并且缺少系统.out.println()输出代码中的code>使我更难看到错误。这是一个同样好的答案,但它使用了ArrayList。我同意ArrayList更易于使用,但分配的任务需要一个2D数组。谢谢你的回答!
String filename = "testData.txt";
final int ARRAY_SIZE = 4;
BufferedReader br = null;
List<char[]> data = new ArrayList<char[]>();
try {
br = new BufferedReader(new FileReader(filename));
int characterIndex = 0;
char[] nextRow = null;
int charValue = 0;
// You don't have to know how many characters are in the input file, in advance
while((charValue = br.read()) != -1)
{
char c = (char)charValue;
// Discard newlines
if(c == '\n' || c == '\r')
{
continue;
}
if(characterIndex % ARRAY_SIZE == 0)
{
nextRow = new char[ARRAY_SIZE];
data.add(nextRow);
characterIndex = 0;
}
nextRow[characterIndex++] = c;
}
br.close();
}
catch (IOException e) {
System.out.println("Error opening the file");
}
int rowIndex = 0;
for(char[] row : data)
{
for(int i = 0; i < ARRAY_SIZE; i++)
{
System.out.print("array[" + rowIndex + "][" + i + "] - " + row[i]);
System.out.print("\n");
}
rowIndex++;
}