试图从txt文件填充二维ArrayList

时间:2012-04-27 20:19:31

标签: java multidimensional-array arraylist java.util.scanner

这应该是一个Sudoku Puzzle解算器,我需要使用二维ArrayList来拼图。

我正在尝试使用txt文件中的数字填充ArrayList。我的测试类中的代码可以生成一个9x9的ArrayList,并使用我用来测试二维ArrayList如何工作的循环用数字1-9填充它。

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

当我尝试将存储在txt文件中的数字用于按照读取顺序填充ArrayList时,我的问题出现了。我尝试了这段代码,以便从txt文件中读取数字并将其重复放入ArrayList,这样它就可以将txt文件中的前9个数字放到ArrayList的第一行,然后转到下一个在txt文件中的行以及用于填充这些数字的ArrayList。

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

我使用此行Exception in thread "main" java.util.NoSuchElementException

获得cell = solutionFile.nextInt();

这是sudoku.txt文件

346791528
918524637
572836914
163257489
895143762
427689351
239415876
684372195
751968243

我首先尝试了这个并得到了错误然后我尝试将所有数字放在一行上,所以我的for循环一次只能读取9个数字,但是当我测试打印ArrayList时或者在添加它们之后它应该包含在内的整个ArrayList是空白的。

它没有从文件中读取数字并将它们放入ArrayList中以便打印出来有什么问题?

1 个答案:

答案 0 :(得分:4)

整数可以是多个字符。您将整行读作一个数字,然后在完成九次读取后失败(每行1次)。

您可以重新格式化您的文本文件,以便分解数字:(我相信如果不是新行,空格将会起作用)

所以喜欢

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

或者您可以阅读整个数字并自行解析

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

或者您可以将值读取为字符串并解析它

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

这不是打印数字列表的有效方法(ArrayList的toString不打印您必须手动执行的列表)

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

应该像

 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
 }