Icegoten Icegoten - 18 days ago 5
Java Question

Trying to fill a two dimensional ArrayList from txt file

This is supposed to be a Sudoku Puzzle solver and it is required I use a two dimensional ArrayList for the puzzle.

I'm trying to fill an ArrayList using numbers from a txt file. The code in my test class can make a 9x9 ArrayList and fill it with numbers 1-9 using the loop I made to test how filling a two dimensional ArrayList works.

import java.util.*;
import java.io.*;

public class test
{
public static void main(String args[])
{
ArrayList<ArrayList<Integer>> data = new ArrayList<ArrayList<Integer>>();

//Loop to add 9 rows
for(int i=1; i<=9; i++)
{
data.add(new ArrayList<Integer>());
}

//Loop to fill the 9 rows with 1-9
for(int k=0; k<9; k++)
{
for(int j=1; j<=9; j++)
{
data.get(k).add(j);
}
}

//Loop to print out the 9 rows
for(int r=0; r<9; r++)
{
System.out.println("Row "+(r+1)+data.get(r));
}

//Reads the file. Need to use this to set the array
File file = new File("F:\\Data Structures 244\\FINAL PROJECT\\SudokuSolver\\sudoku.txt");

try {

Scanner scanner = new Scanner(file);

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}


My problem comes when I try to take the numbers I have stored in the txt file and use them to fill the ArrayList in the order they are read. I tried this code so that it would read the number from the txt file and place it into the ArrayList by having it repeat so it would do the first 9 numbers from the txt file into the first row of the ArrayList and then go to the next row in the txt file as well as the ArrayList to fill those numbers.

File file = new File("F:/Data Structures 244/FINAL PROJECT/SudokuSolver/sudoku.txt");

//Needs this try and catch
try {
Scanner solutionFile = new Scanner(file);
int cell=0;
while (solutionFile.hasNextInt())
{
//Loop to fill the 9 rows with the numbers from the sudoku.txt
for(int k=0; k<9; k++)
{
for(int j=1; j<=9; j++)
{
cell = solutionFile.nextInt();
data.get(k).add(cell);
}

}
}
solutionFile.close();
}

catch (FileNotFoundException e)
{
e.printStackTrace();
}

for(int r=0; r<9; r++)
{
System.out.println("Row "+(r+1)+data.get(r));
}


I get an
Exception in thread "main" java.util.NoSuchElementException
with this line
cell = solutionFile.nextInt();


This is the sudoku.txt file

346791528
918524637
572836914
163257489
895143762
427689351
239415876
684372195
751968243


I tried it like that at first and get that error but then I try taking all of the numbers and putting them on one line so my for loops will only read them 9 numbers at a time but when I test to print the ArrayList or what should be in it after it adds them the entire ArrayList is blank.

What is the problem with it not reading the numbers from the file and putting them into the ArrayList so it will print?

Answer

An integer can be more than one character. You're reading the entire line as a single number and then failing after you finish the nine reads (1 for each line).

You can reformat your text file so that the numbers are broken up: (I believe a space will work if not a new line definitely will)

so like

3 4 6 7 9 1 5 2 8
...

Or you can read the whole number and parse it yourself

int temp = solutionFile.nextInt();

for(int i = 8; i > 0; i--) {
   int cell = temp / (10 * i);
   data.get(k).add(cell);
}

//add the last cell
int cell = temp % 10;
data.get(k).add(cell);

Or you could read the value as a string and parse it

String line = solutionFile.next();

for(int i = 0; i < line.length; i++) {
   Integer cell = Integer.parseInt(line.substring(i, i+1));
   data.get(k).add(cell);
}

This isn't a valid way to print a list of numbers (ArrayList's toString doesn't print the list for you you have to do it manually)

 for(int r=0; r<9; r++)
    {
        System.out.println("Row "+(r+1)+data.get(r));
    }

Should be something like

 for(int r = 0; r < data.size(); r++)
 {
    System.out.print("Row " + (r+1) + ": ");
    for(Integer num : data.get(r)) {
       System.out.print(num + " ");
    }
    System.out.println(); //to end the line
 }