1

I'm trying to read a text file into a 30x30 char array. Scanner does not have a nextChar method, so I'm assuming I'm going to use next() and then split that line into chars? I'm hung up on using 3 for loops to do that though, one for the String, one for rows, and one for columns.

Here is my code so far..

public static void main(String[] args) throws FileNotFoundException {
    final int cellSize = 30; // grid size
    char[][] chargrid = new char[cellSize][cellSize];
    File inputFile = new File("E:\\workspace\\Life2\\bin\\Sample input.txt");
    Scanner in = new Scanner(inputFile);

    char testchar;
    while(in.hasNext()){
    String s = in.next();
    for (int i=0; i<s.length();i++){
     testchar = s.charAt(i);

Now would I do the 2 for statements for the array row & columns and then set chargrid[i][j] = testchar for example?

Any help would be appreciated.

3
  • Why are you trying to read a text file into a 30x30 char array and why not simply into a List<String>? Commented Mar 7, 2012 at 17:11
  • What I really wanted to do is have a 30x30 boolean array where if the char='X' then that cell would be true, but I can't even get it to read the text file into an array right, let alone do that. Commented Mar 7, 2012 at 17:15
  • In that case I suggest first read the file in a List<String> and then examine this List<String> to populate your 30x30 boolean array. Commented Mar 7, 2012 at 17:57

1 Answer 1

0

as far as I saw in your comment you would like to have also an 2D array with boolean if the character is 'X' so I filled two arrays in my code - one actually with the character and one with true or false, depending of the character is 'X' or not. There are also some system.outs to better understand how it works. I am removing newline characters ('\n') when appearing (dont know if you want that or not)

public static void main(String[] args) {
    final int cellSize = 30; // grid size
    char[][] chargrid = new char[cellSize][cellSize];
    boolean[][] chargridIsX = new boolean[cellSize][cellSize];
    File inputFile = new File("input.txt");
    FileInputStream is = null;
    try {
        is = new FileInputStream(inputFile);
        int n = -1;
        int rowCount = 0;
        int colCount = 0;
        while( (n=is.read()) !=-1){
            char readChar = (char) n;
            if(readChar!='\n'){//This removes the linebreaks - dont know if you want that
                chargrid[rowCount][colCount] = readChar;
                if(readChar=='X') { // Here actually set true or false if character is 'X'
                    chargridIsX[rowCount][colCount] = true;
                    System.out.println("row "+rowCount+" col "+colCount + " = " + readChar + " " + true);
                }else {
                    chargridIsX[rowCount][colCount] = false;
                    System.out.println("row "+rowCount+" col "+colCount + " = " + readChar+ " " + false);
                }
                if(rowCount++ >= cellSize-1){
                    System.out.println("new col");
                    rowCount = 0;
                    if(colCount++ >= cellSize-1){
                        //ARRAY IS FULL
                        System.out.println("full");
                        break;
                    }
                }
            }
        }

    } catch (FileNotFoundException e) {
        //could not get Inputstream from file
        e.printStackTrace();
    } catch (IOException e) {
        // problem with the inputstream while reading
        e.printStackTrace();
    }

btw. I am reading character for character from an inputstream instead of using scanner, hope that is okay - otherwise let me know

For what ever this might be good

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.