[英]Java - Reading from text file using scanner into a 2D char array
I'm trying to read a text file into a 30x30 char array. 我正在尝试将文本文件读入30x30 char数组。 Scanner does not have a nextChar method, so I'm assuming I'm going to use next() and then split that line into chars?
扫描器没有nextChar方法,所以我假设我要使用next()然后将该行拆分为char? I'm hung up on using 3 for loops to do that though, one for the String, one for rows, and one for columns.
我挂了使用3 for循环来做到这一点,一个用于字符串,一个用于行,一个用于列。
Here is my code so far.. 到目前为止,这是我的代码。
public static void main(String[] args) throws FileNotFoundException {
final int cellSize = 30; // grid size
char[][] chargrid = new char[cellSize][cellSize];
File inputFile = new File("E:\\workspace\\Life2\\bin\\Sample input.txt");
Scanner in = new Scanner(inputFile);
char testchar;
while(in.hasNext()){
String s = in.next();
for (int i=0; i<s.length();i++){
testchar = s.charAt(i);
Now would I do the 2 for statements for the array row & columns and then set chargrid[i][j] = testchar for example? 现在,我将对数组行和列执行2 for语句,然后例如设置chargrid [i] [j] = testchar吗?
Any help would be appreciated. 任何帮助,将不胜感激。
as far as I saw in your comment you would like to have also an 2D array with boolean if the character is 'X' so I filled two arrays in my code - one actually with the character and one with true or false, depending of the character is 'X' or not. 据我在您的评论中看到的,如果字符是“ X”,您还希望有一个带有布尔值的2D数组,因此我在代码中填充了两个数组-一个实际上是字符,另一个是true或false,具体取决于字符是否为“ X”。 There are also some system.outs to better understand how it works.
还有一些system.outs可以更好地了解它是如何工作的。 I am removing newline characters ('\\n') when appearing (dont know if you want that or not)
我要在出现时删除换行符('\\ n')(不知道是否要这样)
public static void main(String[] args) {
final int cellSize = 30; // grid size
char[][] chargrid = new char[cellSize][cellSize];
boolean[][] chargridIsX = new boolean[cellSize][cellSize];
File inputFile = new File("input.txt");
FileInputStream is = null;
try {
is = new FileInputStream(inputFile);
int n = -1;
int rowCount = 0;
int colCount = 0;
while( (n=is.read()) !=-1){
char readChar = (char) n;
if(readChar!='\n'){//This removes the linebreaks - dont know if you want that
chargrid[rowCount][colCount] = readChar;
if(readChar=='X') { // Here actually set true or false if character is 'X'
chargridIsX[rowCount][colCount] = true;
System.out.println("row "+rowCount+" col "+colCount + " = " + readChar + " " + true);
}else {
chargridIsX[rowCount][colCount] = false;
System.out.println("row "+rowCount+" col "+colCount + " = " + readChar+ " " + false);
}
if(rowCount++ >= cellSize-1){
System.out.println("new col");
rowCount = 0;
if(colCount++ >= cellSize-1){
//ARRAY IS FULL
System.out.println("full");
break;
}
}
}
}
} catch (FileNotFoundException e) {
//could not get Inputstream from file
e.printStackTrace();
} catch (IOException e) {
// problem with the inputstream while reading
e.printStackTrace();
}
btw. 顺便说一句 I am reading character for character from an inputstream instead of using scanner, hope that is okay - otherwise let me know
我正在从输入流而不是使用扫描仪来读取字符,希望可以-否则让我知道
For what ever this might be good 对于这可能是一件好事
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.