我正在創建一個程序,它將讀取一個txt文件(來自另一個主文本文件),取一串字符串@ -K),然後使用字符串中字符的ASCII值填充2d數組。所以我已經成功創建了2d數組並讀取了文件。問題是我的數組只是由字符串文件的第一個字母填充,而不是貫穿整個文件。我有一個if語句,我相信應該通過該文件讀取並正確填充它,但它沒有這樣做。無法弄清楚我做錯了什麼。這是代碼。從一個字符串填充一個二維數組,並將單個字符的ASCII值放入它
public static void main(String[] args) throws IOException
{
String txtfile;
String txtfilecontents;
int matrix[][]= new int [24][34];
int row=0;
int col=0;
BufferedReader masterfile = new BufferedReader(new FileReader("imageFileList.txt"));
while((txtfile=masterfile.readLine())!=null) //reads master text file
{
BufferedReader imagefile= new BufferedReader(new FileReader(txtfile));
while((txtfilecontents=imagefile.readLine())!=null) //reads text file within master file
{
for(int i=0;i<txtfilecontents.length();i++)
{
if(col ==34)
{
col = 0;
row++;
}
if(row ==24)
{
col=0;
row=0;
break;
}
matrix[row][col] = (int)txtfilecontents.charAt(i)-64; //instead of adding every character it only adds the first one until it fills up the 2d array
col++;
}
System.out.println(txtfilecontents);
for(row=0;row<24;row++)
{
for(col=0;col<34;col++)
System.out.printf("%4d",matrix[row][col]);
System.out.println();
}
}
System.out.println(txtfile);
}
}
下面是txtfile在其中的示例。
[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@AGDFE[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@FH
而且我已經加入了一堆文件中打印報表的,所以我可以看看我的輸出來臨了,但它會在以後取出來。
給出的示例輸入,又該期望輸出是什麼?此外,減去64將不會給你任何東西的ASCII值... – nhgrif
示例輸出應該是用輸入的ascii值填充的2d數組。減去64只是爲了讓我得到0-11的值(@ ASCII值是64,減去64 = 0等) – Student
所以你的輸出應該是ASCII值還是ASCII值減去64 ...? – nhgrif