Ahmed Hadi
Ahmed Hadi

Reputation: 121

How to read multiple lines with more that one elements in a line from an array in java?

Here is what i've done so far: but you should look at the print-screen to see what output i'm getting. and here is the elements in the .txt file that i trying to read from:

1000
1500 2
750
600
500 3
100
250
750
600 5
1500
400
500 4
1000
1500
750
-1
500
1500

just to let you know what this is, it is a Whammy game and the above numbers are the values that my spin() randomly pick from.

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 according your advice with editing the IF **********

int cellLine = Integer.parseInt(line); // to be passd to Cell constructor int SpecialCell = Integer.parseInt(line); // to be passed to Special Cell constructor

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

Now i'm getting --> Exception in thread "main" java.lang.NumberFormatException: For input string: "1500 2"

Upvotes: 0

Views: 943

Answers (2)

Alessandro Scarlatti
Alessandro Scarlatti

Reputation: 734

I think you want both values from the original line, right? You need to use the individual strings that you split using lines.split();

And perhaps try the logic like this:

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
}

Upvotes: 1

Alessandro Scarlatti
Alessandro Scarlatti

Reputation: 734

It looks like the block beginning on line 33 will only execute should the array contain 1 element, which is exactly what you don't want it to do. Move that block outside the first if block so that it can execute. Better yet, do

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

So specifically:

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

Upvotes: 2

Related Questions