2016-11-26 5 views
0

これまで行ってきたことがありますが、プリント画面を見て、どのような出力が得られているのかを確認する必要があります。そしてここで私はから読み取るしようとしている.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」

+1

なぜあなたはここにコードを追加することはできません? – developer

+0

私は印刷画面に注釈を付したので説明がありますが、コードをすぐに投稿します... –

+1

IDEを使用してコードを自動的にインデントし、問題の1つを直ちに表示する必要があります。 2つ目の問題は、2つの値を整数として含む行を解析しようとしていることです。それはあまり意味がありません。 –

答えて

1

は、私はあなたが正しい、元のラインからの両方の値をしたいと思いますか?あなたはlines.split();

を使用して分割し、おそらくこのようにロジックを試してみてください個々の文字列を使用する必要があります。

int firstValue = 0;  // initialize a first value (always used) 
int secondValue = 0;  // initialize a second value (may or may not be used) 

if(lineArr.length == 1){ 
     // parse just the first element. That's all we need. 
     firstValue = Integer.parseInt(lineArr[0]); 
     // do stuff here... 
} 
if(lineArr.length == 2){ 
     // parse both elements. We need both, right? 
     firstValue = Integer.parseInt(lineArr[0]); 
     lineArr[1] = lineArr[1].trim(); // remove any leading whitespaces! 
     secondValue = Integer.parseInt(lineArr[1]); 
     // do stuff here... 
} 

EDIT

if(lineArr.length == 1){ 
     // parse just the first element. That's all we need. 
     firstValue = Integer.parseInt(lineArr[0]); 
     // do stuff here... 
} 
if(lineArr.length > 1){ 
     firstValue = Integer.parseInt(lineArr[0]); 
     for (int i = 1; i < lineArr.length; ++i) { 
      if (!lineArr[i].equals("")) { 
       secondValue = Integer.parseInt(lineArr[i]); 
      } 
     } 
     // do stuff here with firstValue and secondValue 
} 
+0

恐れて、私は非常に感謝しています、それは働いた! 2つの値を持つ要素の最初の値を出力しますが、スペースで区切られた別の値は出力されません。 –

+0

'lines.split(" ");'は、送信した区切り文字を正確に見つけた場合にのみ、第2の値を探します。したがって、複数のスペースがあった場合、 '123 456'のように動作しません。これを修正するには、 'lineArr [1] = lineArr [1] .trim();' –

+0

あなたは私があなたにどれほど感謝しているのか分かりません。私はすべての2つの要素の製品を取得しようとしていると私はあなたに問題があった場合は、メンター:) –

2

に見えます>スレッドの例外「メイン」java.lang.NumberFormatExceptionを - //は、今私が取得しています特別なセル・コンストラクタ

  if(lineArr.length == 1 && lineArr.length == 2){ 
       Cell cellObject = new Cell(cellLine); 
       SpecialCell specialCellObject = new SpecialCell(cellLine, SpecialCell); 
       cellList.add(cellObject); 
       cellList.add(specialCellObject); 

に渡されます33行目から始まるブロックのように、配列には1つの要素が含まれている場合にのみ実行されます。これはまさにあなたがしたくないことです。そのブロックを最初のifブロックの外に移動して実行します。いっそのこと、だから、特に

if (lineArray.length == 1) { 
    // code for one element here... 
} 
if (lineArray.length == 2) { 
    // code for two elements here... 
} 

の操作を行います。

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(); 
} 
+0

私がそうするとき、2番目のif内のcellLineはアクセスできません。私はそれをspecialCellObjectコンストラクタに渡していることに注意してください。 –

+0

配列は1または2の長さしか持てないが、両方はできないので、 'else if'は不要であるように思えます。@ TIMBiegeleisen Right。 –

+0

ありがとう! –

関連する問題