[英]reading txt file with numbers and saving in it in a String Matrix
Im trying to read a txt file with numbers and saving each number in a String matrix but there some things i dont get. 我试图读取带有数字的txt文件并将每个数字保存在String矩阵中,但有些事情我没有得到。
1.-If i run the code in Eclipse i get printed like 16 times everything and the numbers get smaller and smaller.. 1.-如果我在Eclipse中运行代码,我将得到16倍的打印结果,并且数字越来越小。
2.-After beeing printed there is a null value on the bottom . 2.-在打印蜂后,底部有一个空值。 why?
为什么?
3.-Is it ok that the variable j is beeing defined outside the while loop? 3.-可以在while循环之外定义变量j吗? or should it be inside?
还是应该在里面?
Thanks fot the help! 谢谢您的帮助!
I wrote this so far: The text file is contains the following: 到目前为止,我已经写了:文本文件包含以下内容:
0000000000000000
0000000000000000
0000000400000000
0001111111160000
0001115111110000
0001161121510000
0001111511110000
0001110001110000
0000011311000000
0000011111000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
and the programm: 和程序:
package Tests;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
public class Test2 {
public static void main(String [] args){
try {
File fil = new File("/Users/MaxRuizTagle/Desktop/AbschlussprojektWiSe1415/Canberra Fox - The Key Huntress/lvl/level0.txt");
FileReader inputFil = new FileReader(fil);
BufferedReader in = new BufferedReader(inputFil);
String s = in.readLine();
int largo = s.length();
String [][] matrix = new String [largo][largo];
String line;
int j=0;
while ((line=in.readLine() ) != null){
for (int i=0;i<16;i++){
matrix[i][j] = line.substring(i);
}
j++;
}
for(int i=0;i<16;i++){
for( j=0;j<16;j++){
System.out.println(matrix[i][j]+" ");
}
System.out.println("");
}
}catch (IOException e1){
e1.printStackTrace();
}
}
}
1) The reason that 16 blocks of numbers are printed is that you print 16 blocks of numbers here: 1)打印16个数字块的原因是您在此处打印16个数字块:
for(int i=0;i<16;i++){
for( j=0;j<16;j++){
System.out.println(matrix[i][j]+" "); // <-- each one a line.
}
System.out.println("");
}
The reason that they get shorter is that you previously saved ever shorter substrings here: 它们变短的原因是您以前在这里保存了越来越短的子字符串:
for (int i=0;i<16;i++){
matrix[i][j] = line.substring(i); // <-- substring from pos i to end
}
You weren't very clear about your requirements, so I have to guess a little; 您对您的要求不是很清楚,所以我不得不猜测一下。 it looks to me as though what you wanted to do was extract strings of one character each:
在我看来,您想要做的是提取每个字符一个的字符串:
for (int i=0;i<16;i++){
matrix[i][j] = line.substring(i, i + 1); // substring from i to i + 1
}
And later print each column in a line (transposing the image): 然后在一行中打印每一列(转置图像):
for(int i=0;i<16;i++){
for( j=0;j<16;j++){
System.out.print(matrix[i][j]+" "); // print instead of println
}
System.out.println("");
}
Although I wonder why you bother with an array of String
s if you could use a char[][] matrix
and line.charAt(i)
or save the lines as they come and extract individual characters as lines[j].charAt(i)
in the output loop. 虽然我不知道为什么你的数组打扰
String
■如果可以使用char[][] matrix
和line.charAt(i)
或单个字符保存行,因为他们来和提取物作为lines[j].charAt(i)
在输出循环中。
2) You discard a line in the beginning: 2)您在开头删除一行:
String s = in.readLine();
int largo = s.length();
// s is not used after this
because of this, your matrix shrinks from 16x16 to 15x16, and the loop 因此,您的矩阵从16x16缩小到15x16,然后循环
while ((line=in.readLine() ) != null){
...
doesn't initialize the last column of matrix
, which remains filled with null
s. 不初始化
matrix
的最后一列,该列仍为null
填充。 The least invasive fix for that would be to, well, treat the first line in the loop like all others, as in 对此,侵入性最小的修复方法是将循环中的第一行与其他所有行一样对待,就像在
String line = s;
while(line != null) {
...
line = in.readLine();
}
3) I see no problem with that. 3)我认为没有问题。
4) The way you treat your array dimensions is a bit odd. 4)处理数组维数的方法有些奇怪。 You appear to read them from the file, but later they're hard-coded.
您似乎从文件中读取了它们,但后来对其进行了硬编码。 If your code is only going to work with a 16x16 matrix, why do you bother with
largo
, and if it should work with matrices of other sizes, why is the size of 16 hardcoded everywhere else? 如果您的代码仅适用于16x16矩阵,那么为什么要打扰
largo
呢?如果它应该与其他大小的矩阵一起工作,为什么在其他地方都硬编码16的大小呢?
2.-After beeing printed there is a null value on the bottom .
2.-在打印蜂后,底部有一个空值。 why?
为什么?
Because the first line is not put in the matrix, you only use that for s.length()
. 由于第一行未放入矩阵中,因此仅将其用于
s.length()
。 Then the while
loop will use only the 15 remaining lines, instead of 16. As a result, the last j
index will have null
values. 然后,
while
循环将仅使用剩余的15行,而不是16行。因此,最后一个j
索引将具有null
值。
3.-Is it ok that the variable j is beeing defined outside the while loop?
3.-可以在while循环之外定义变量j吗? or should it be inside?
还是应该在里面?
It's not great to be outside and reused. 不在外面并重新使用不是很好。 It's better to declare it inside.
最好在内部声明它。
Maybe you're looking for something more like this: 也许您正在寻找更像这样的东西:
for (int j = 0; j < 16; j++) {
matrix[0][j] = String.valueOf(s.charAt(j));
}
for (int i = 1; (line = in.readLine()) != null; ++i) {
for (int j = 0; j < 16; j++) {
matrix[i][j] = String.valueOf(line.charAt(j));
}
}
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println("");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.