これまで行ってきたことがありますが、プリント画面を見て、どのような出力が得られているのかを確認する必要があります。そしてここで私はから読み取るしようとしている.txtファイル内の要素です:ちょうどあなたはこれが何であるかを知っているようにjavaの配列から1行に1つ以上の要素を含む複数の行を読み込む方法は?
1000
1500 2
750
600
500 3
100
250
750
600 5
1500
400
500 4
1000
1500
750
-1
500
1500
は、)それはワーミーゲームで、上記の数字は私のスピンは(値であり、ランダムに選んでください。
public class Board {
ArrayList<Cell> cellList = new ArrayList<>(); // creating an arraylist that holds the array elemetns in it.
public Board() throws FileNotFoundException {
java.io.File file = new java.io.File("CellValues.txt");
System.out.println("isFile: " + file.exists());
try (java.util.Scanner scan = new java.util.Scanner(file)) {
while(scan.hasNext()){
String line = scan.nextLine();
String[] lineArr = line.split(" ");
System.out.println(lineArr.length); // test number number of index
if(lineArr.length == 1){
int cellLine = Integer.parseInt(line); // to be passd to Cell constructor
Cell cellObject = new Cell(cellLine);
cellList.add(cellObject);
if(lineArr.length == 2){
int SpecialCell = Integer.parseInt(line); // to be passed to Special Cell constructor
SpecialCell specialCellObject = new SpecialCell(cellLine, SpecialCell);
cellList.add(specialCellObject);
//cellLine *= SpecialCell;
//int x = cellObject.getValue() * (int) specialCellObject.getMultiplier();
}
}
}
for(Cell i : cellList){
System.out.println(i);
}
System.out.println("\ncurrent elements in the arraylist are: " + cellList.size());
scan.close();
}
}
******** UPDATE ********** IFを編集してあなたのアドバイスに従って
int型cellLine = Integer.parseInt(ライン); // Cellコンストラクタに渡されます。 int SpecialCell = Integer.parseInt(line);入力文字列の場合:「1500年2」
なぜあなたはここにコードを追加することはできません? – developer
私は印刷画面に注釈を付したので説明がありますが、コードをすぐに投稿します... –
IDEを使用してコードを自動的にインデントし、問題の1つを直ちに表示する必要があります。 2つ目の問題は、2つの値を整数として含む行を解析しようとしていることです。それはあまり意味がありません。 –