magna_nz
magna_nz

Reputation: 1273

reading strings until blank line then reading more

I'm writing a program which reads in lines from a file with Scanner. When there is a blank line, I want to process the lines already seen and stored in a String array list. Then I want it to skip to the next line in the file until it sees a blank line again.. then process those lines, until the file is fully read.

I can't seem to wrap my head around doing this? So far my program works for the set of lines up until it reaches the blank line but then doesn't progress.

The processing I want to do can be seen in my code after the while scan.hasnextLine() loop has finished.

Thanks for your help.

public static void main(String[]argv){
    Scanner scan = new Scanner(System.in);
    //read in the line from stdin, if it's empty, then break out OR process it
    while (scan.hasNextLine()){
        String tempLine = scan.nextLine();
        if (tempLine.equals("")){
            break;
        }        
        columnCount = tempLine.length();
        tempArray.add(tempLine); //add temp line to string array initial

        rowCount++;
    }
    System.out.println("rowcount: "+rowCount);
    System.out.println("columnCount: "+columnCount);

    //set the 2D array to the right size
    epidemicArray = new char[rowCount][columnCount];

    //populate 2d array with strings from tempArray
    updateArray();
    System.out.println("original array:");
    printArray();

    //copy populated 2d array to another array
    copyArray(epidemicArrayCopy);


    //iterate over array to get final state
    do{
        getFinalState(epidemicArray);
    } while (countChanges > 0);
    printArray();

}

Upvotes: 2

Views: 3883

Answers (3)

Joseph118
Joseph118

Reputation: 505

The issue that you have is that you are using break to get out of a loop while what you really want is to skip the line and continue.

So basically all you have to do is remove the break and alter a bit of your code to look similar to this:

String tempLine = scan.nextLine();
if (!tempLine.equals("")){
    columnCount = tempLine.length();
    tempArray.add(tempLine); //add temp line to string array initial

    rowCount++;
}        

Upvotes: 1

csga5000
csga5000

Reputation: 4151

I think this will work.

public static void main(String[]argv){
    Scanner scan = new Scanner(System.in);
    //read in the line from stdin, if it's empty, then break out OR process it

    while (readSome(scan)){}
}

public void readSome(Scanner scan) {
    while (true){
        if (!scan.hasNextLine()) {
            return false;
        }
        String tempLine = scan.nextLine();
        if (tempLine.equals("")){
            break;
        }
        columnCount = tempLine.length();
        tempArray.add(tempLine); //add temp line to string array initial

        rowCount++;
    }
    System.out.println("rowcount: "+rowCount);
    System.out.println("columnCount: "+columnCount);

    //set the 2D array to the right size
    epidemicArray = new char[rowCount][columnCount];

    //populate 2d array with strings from tempArray
    updateArray();
    System.out.println("original array:");
    printArray();

    //copy populated 2d array to another array
    copyArray(epidemicArrayCopy);


    //iterate over array to get final state
    do{
        getFinalState(epidemicArray);
    } while (countChanges > 0);
    printArray();

    return true;
}

Here we do your processing on each chunk read.

EDIT: I suspect you need to add:

columnCount = 0;
rowCount = 0;

to the beginning of the readSome method.

End-edit

Edit 2

You probably need this too:

tempArray = new ArrayList<String>();

Near the column/row count resets.

End edit 2

Or maybe you wanted:

public static void main(String[]argv){
    Scanner scan = new Scanner(System.in);
    //read in the line from stdin, if it's empty, then break out OR process it
    while (scan.hasNextLine()){
        String tempLine = scan.nextLine();
        if (tempLine.equals("")){
            continue;
        }
        columnCount = tempLine.length();
        tempArray.add(tempLine); //add temp line to string array initial

        rowCount++;
    }
    System.out.println("rowcount: "+rowCount);
    System.out.println("columnCount: "+columnCount);

    //set the 2D array to the right size
    epidemicArray = new char[rowCount][columnCount];

    //populate 2d array with strings from tempArray
    updateArray();
    System.out.println("original array:");
    printArray();

    //copy populated 2d array to another array
    copyArray(epidemicArrayCopy);


    //iterate over array to get final state
    do{
        getFinalState(epidemicArray);
    } while (countChanges > 0);
    printArray();

}

The difference here is that we continue, not break, so we read the rest of the lines. But we only process after reading the whole file.

Upvotes: 1

RockOnRockOut
RockOnRockOut

Reputation: 761

Here's where your problem happens:

if (tempLine.equals("")){
    break;
}        

When you get to break you exit the loop. Make a function called processArraySoFar and call that instead of break. Assuming tempArray is an ArrayList:

if(tempLine.equals("")){
    processArraySoFar(tempArray);
    tempArray = new ArrayList(); //make a fresh ArrayList to use until the next line
}

Upvotes: 2

Related Questions