0

This is similar to Reading 2-D array from a file. I'm trying to read a text file into a 2-D array in java. The difference is that I need it to be read in as an array of strings instead of ints. (Yes, this is homework.) I was able to get the code to work for ints but my text file can have "*" and "-" in it, which causes the .nextInt() to throw an exception. I've tried using the .next() to pull it as a string using a blank space as a delimiter. However this is also throwing an exception from the start. The problem seems to be in the readFile function. How can I pull in each character as a string? Here's my 3 functions:

    public static void main(String [] args) {

        Scanner s = new Scanner(System.in);
        String fileName = ""; //for javac
        //obtain file name for puzzle
        if(0 == args.length) {
            System.out.println("Welcome to the Sudoku Solver.");
            System.out.println("Please enter a file name.");
            fileName = s.nextLine();
        } else if(1 == args.length) {
            fileName = args[0];
        } else {
            System.out.println("You have entered invalid data.");
            System.exit(1);
        }

        //open puzzle file and read puzzle
        int m = 0; //for javac
        try {
            BufferedReader f = new BufferedReader(new FileReader(fileName));

            while(f.readLine() != null) {
                ++m;
            }
            System.out.println(m);
            String[][] theArray; 
            f.close();

            theArray = readFile(m, fileName);
            readPuzzle(theArray);

        } catch(Exception e) {
            System.out.println("An error has occurred...");
        }
    }

    public static void readPuzzle(String [][] thePuzzle) {

        for(int r = 0; r < thePuzzle.length; ++r) {
            for(int c = 0; c < thePuzzle[r].length; ++c) {
                System.out.printf("%7d", thePuzzle[r][c]);
            }
            System.out.println();
        }
        System.out.println();
    }

    public static String[][] readFile(int m, String fileName) {

        String[][] theArray = new String[m][m];
        try{
            Scanner g = new Scanner(new File(fileName));

            for(int r = 0; r <= theArray.length; ++r){
                for(int c = 0; c <= theArray[r].length; ++c){
                    if(g.hasNext()){
                        theArray[r][c] = g.next("\\s+");
                    }
                }
            }
        } catch(Exception e) {
            System.out.println("Error in readFile.");
        }

        return theArray;
    }

The text file looks like this:

    5 3 * * 7 * * * *
    6 * * 1 9 5 * * *
    * 9 8 * * * * 6 *
    8 * * * 6 * * * 3
    4 * * 8 * 3 * * 1
    7 * * * 2 * * * 6
    * 6 * * * * * * *
    * * * 4 1 9 * * 5
    * * * * 8 * * 7 9
Community
  • 1
  • 1
wxwatchr
  • 1
  • 2

2 Answers2

0

I think instead of using Scanner.next(), may be you want to try combine BufferReader and String.split.

Here is the sample code:

BufferedReader f = new BufferedReader(new FileReader(fileName));
String[][] theArray = new String[9][9];  // << change array size as you wish
String line;
while((line = f.readLine()) != null) {
    theArray[m] = line.split(" ");   // Split text line by space
    ++m;
}
Minh
  • 424
  • 3
  • 12
  • I've added this to my code but I'm still getting an exception thrown every time I try to use it. Any ideas why it is throwing the exception? – wxwatchr Sep 17 '16 at 17:40
-1

Below code will work similar to what are you expecting. Please make sure that data file doesn't contain any unnecessary spaces. Since size of array is hard coded for now so you have to keep in mind to keep size (of array in code and data in file) in sync as well.


public static void main(String[] args) { try { Scanner input = new Scanner(new FileReader(new File("D:/Data File/Data.txt"))); int iRows = 9; int iCols = 9; String [][] strArr = new String[iRows][iCols]; for(int i = 0; i < iRows ; i++){ for(int j = 0; j < iCols ; j++){ strArr[i][j] = input.next(); } } input.close(); //Printing for(int i = 0; i < 9 ; i++){ for(int j = 0; j < 9 ; j++){ System.out.print(strArr[i][j] + " "); } System.out.println(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

Ashish Kumar
  • 916
  • 2
  • 15
  • 32