Question

So im trying to count how many rows and columns an input file of a completed sudoku board has. I've come up with this loop

public static Pair<Integer, Integer> rowColumnCount(Scanner input1){
    int nrows=0;
    int ncolumns=0;


   while(input1.hasNextLine()){

       while(input1.hasNextInt()){
           input1.nextInt();
           ncolumns++;
       }

        input1.nextLine();
        nrows++;
    }

    System.out.print("Rows: " +nrows + "\n");
    System.out.print("Columns: " +ncolumns + "\n");

    return new Pair<Integer, Integer>(nrows, ncolumns);

    }//end rowCoulmnCount

I got the idea to make this method from a previous method I have in which I read and created an array of the sudoku board input file. I figured the two methods would be similar, but they're not.. the outpuut of nrws and ncolumns is 1 & 81. So could someone help me figure out the proper way to count columns to make sure there are 9. Or would it be better to start comparing the values of the rows and columns to see if there are any duplicates (of 1-9) in use that error checking to see if there are the correct number of rows and columns?

Was it helpful?

Solution

try:

public static Pair<Integer, Integer> rowColumnCount(Scanner input1){
    int nrows=0;
    int ncolumns=0;


   while(input1.hasNextLine()){
       Scanner subinput1 = new Scanner(input1.nextLine());
       while(subinput1.hasNextInt()){
           subinput1.nextInt();
           ncolumns++;
       }

        nrows++;
    }

    System.out.print("Rows: " +nrows + "\n");
    System.out.print("Columns: " +ncolumns + "\n");

    return new Pair<Integer, Integer>(nrows, ncolumns);

    }//end rowCoulmnCount
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top