次のようなテキスト ファイルがあります。
5 10
ahijkrewed
llpuvesoru
irtxmnpcsd
kuivoqrsab
eneertlqzr
tree
alike
cow
dud
able
dew
最初の行の最初の数字 5 は単語検索パズルの行数で、10 はパズルの列数です。次の 5 行はパズルです。行の整数に 5 を、列の整数に 10 を配置する方法を知る必要があります。次に、次の行にスキップして文字列を読み取る必要があります。パズルの 5 行のみの修正ファイルを使用して、パズル部分を 2 次元配列に配置する方法を見つけましたが、適切なテキスト ファイルの最初の行からその配列のサイズを設定する方法が必要です。
私はこれを書きました:
import java.io.*;
class SearchDriver {
public static void processFile(String filename) throws FileNotFoundException, IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader in = new BufferedReader(fileReader);
// declare size of array needed
//
// int rows and columns need to be read in from first
// line of the file which will look like: X Y
//
// so i need rows = X and columns = Y
int rows = 5;
int columns = 10;
String[][] s = new String[rows][columns];
// start to loop through the file
//
// this will need to start at the second line of the file
for(int i = 0; i < rows; i++) {
s[i] = in.readLine().split("(?!^)");
}
// print out the 2d array to make sure i know what i'm doing
for(int i=0;i<rows;i++) {
for(int j=0;j<columns;j++) {
System.out.println("(i,j) " + "(" + i + "," + j + ")");
System.out.println(s[i][j]);
}
}
}
public static void main(String[] args)
throws FileNotFoundException, IOException {
processFile("puzzle.txt");
}
}
BufferedReader を使用したファイルの読み取りに関する例と広範なドキュメントを含む Web サイトを含め、任意の助けをいただければ幸いです。