Results 1 to 9 of 9
  1. #1
    Faytrow is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Issue with reading from a file with a scanner and incrementing an int[][]

    Okay, so I'm working on a mine-sweeper-esque program for a class (minus the GUI) where I essentially read off information from a pre-constructed .txt document to tell the programs how big the array is(square only), where the bombs are, and then adds in more bombs to exact coordinates after the initial board is completed. I got it just about all written out, but for some darn reason I'm having 2-3 main problems (I say 2-3 because only 2 of them are actually ailing me, however I don't understand why one of them won't work. Also, they may all be related). I've been at this for about 4 hours and there's just a couple itty bitty things I think I'm missing or don't understand and would really appreciate some help.

    Here's the code for the MineField class
    Java Code:
    package cs1120lab1;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class MineField {
    
        //array containing mine locations and mine counters
        int[][] mineField;
        //Reads information from imported .txt file
        Scanner input;
    
        /**
         * In lab report this method is called GenerateFromFile.
         */
        public MineField(String str) throws FileNotFoundException, InterruptedException {
            input = new Scanner(new File(str));
            //Finds the first integer in the input file and sets it to variable numericFieldSize.
            String fieldSize = input.nextLine();
            int numericFieldSize = Integer.parseInt(fieldSize);
    
            //Creates the int-array mineField
            mineField = new int[numericFieldSize][numericFieldSize];
    
    //places 0s and -1s corresponding to their respective locations in the grid from the .txt document
            for (int i = 0; i < numericFieldSize; i++) {
                for (int j = 0; j < numericFieldSize; j++) {
                    //places a 0 corresponding to all '.'s
                    if (input.hasNextLine() && input.nextLine().equalsIgnoreCase(".")) {
                        mineField[i][j] = 0;
                    } //and a -1 corresponding to all 'X's
                    else {
                        mineField[i][j] = -1;
                    }
                }
            }
    //looks through the array for a bomb (-1) if it finds one, then increments all non-bombs around it (up to 8 spaces).
            for (int i = 0; i < numericFieldSize; i++) {
                for (int j = 0; j < numericFieldSize; j++) {
                    //located a bomb
                    if (mineField[i][j] == -1) {
                        //increments above and to the left
                        if (i > 0 && j > 0 && mineField[i - 1][j - 1] != -1) {
                            mineField[i - 1][j - 1] += 1;
                        }
                        //increments straight above
                        if (i > 0 && mineField[i - 1][j] != -1) {
                            mineField[i - 1][j] += 1;
                        }
                        //increments above and to the right
                        if (i > 0 && j < mineField.length-1 && mineField[i - 1][j + 1] != -1) {
                            mineField[i - 1][j + 1] += 1;
                        }
                        //increments to the left
                        if (j > 0 && mineField[i][j - 1] != -1) {
                            mineField[i][j - 1] += 1;
                        }
                        //Increments to the right
                        if (j < mineField.length-1 && mineField[i][j + 1] != -1) {
                            mineField[i][j + 1] += 1;
                        }
                        //Increments to the lower left
                        if (i < mineField.length-1 && j > 0 && mineField[i + 1][j - 1] != -1) {
                            mineField[i + 1][j - 1] += 1;
                        }
                        //Increments straight down
                        if (i < mineField.length-1 && mineField[i + 1][j] != -1) {
                            mineField[i + 1][j] += 1;
                        }
                        //Increments to the lower right
                        if (i < mineField.length-1 && j < mineField.length-1 && mineField[i + 1][j + 1] != -1) {
                            mineField[i + 1][j + 1] += 1;
                        }
    
    
                    }
                }
            }
        }
    
        public void addMine() {
            while (input.hasNextLine()) {
                String X, Y;
                int x, y;
                //sets x and y has coordinates indicated in the imported file.
                X = input.nextLine();
                Y = input.nextLine();
    
                x = Integer.parseInt(X);
                y = Integer.parseInt(Y);
    
                mineField[x][y] = -1;
                //Adds 1 to all surrounding spaces if they are not already mines.
                if (mineField[x - 1][y - 1] != -1) {
                    mineField[x - 1][y - 1] += 1;
                }
                if (mineField[x - 1][y] != -1) {
                    mineField[x - 1][y] += 1;
                }
                if (mineField[x - 1][y + 1] != -1) {
                    mineField[x - 1][y + 1] += 1;
                }
                if (mineField[x][y - 1] != -1) {
                    mineField[x][y - 1] += 1;
                }
                if (mineField[x][y + 1] != -1) {
                    mineField[x][y + 1] += 1;
                }
                if (mineField[x + 1][y - 1] != -1) {
                    mineField[x + 1][y - 1] += 1;
                }
                if (mineField[x + 1][y] != -1) {
                    mineField[x + 1][y] += 1;
                }
                if (mineField[x + 1][y + 1] != -1) {
                    mineField[x + 1][y + 1] += 1;
                }
                //prints that the program is adding mines.
                System.out.printf("Adding mine at %d,%d.\n", x, y);
            }
        }
    
        public void printField() {
            for (int i = 0; i < mineField.length; i++) {
                for (int j = 0; j < mineField.length; j++) {
                    System.out.print(mineField[i][j]);
                    if (j + 1 < mineField.length && mineField[i][j + 1] >= 0) {
                        System.out.print("  ");
                    } else {
                        System.out.print(" ");
                    }
                    if ((j + 1) % mineField.length == 0) {
                        System.out.println();
                    }
                }
            }
        }
    }
    The Main class is pretty boring and just gets the user's input for what the .txt document is called, then creates the class and calls the addMine method once, and the printField method twice. I don't believe there's a problem there so I won't bother posting it, but lemme know if you'd like it.

    Here's the .txt document that it's importing and reading from.

    8
    . . . X . . X .
    . X . . . . . X
    . . . . . . . .
    X X . . . . . .
    . . . . . . X X
    . . X X . X . X
    X . X . X . X .
    . . . . . X . .
    5 , 7
    2 , 5
    6 , 3
    4 , 9


    The two main issues I'm having is right now, the array is coming out as all -1s, and the other one is that within the addMine method, the While boolean is never true, even though there is still eight more integers and four ","s left. I think I may have goofed and assumed that the scanner would start tokeinizing from the same location it ended, but it seems as if the scanner closes after the class is created, otherwise the constructor method is just burning all the way through the document before the addMine method can do anything with it. Anyways, thanks for your help to anyone who is willing to put forth any time into my dilema :)

    P.S. The other weird problem I was having is here.
    Java Code:
    for (int i = 0; i < numericFieldSize; i++) {
                for (int j = 0; j < numericFieldSize; j++) {
                    //places a 0 corresponding to all '.'s
                    if (input.hasNextLine() && input.nextLine().equalsIgnoreCase(".")) {
                        mineField[i][j] = 0;
                    } //and a -1 corresponding to all 'X's
                    else {
                        mineField[i][j] = -1;
                    }
    If I set the "else" statement to an "else if ( input.hasNextLine() && input.nextLine().equalsIgnoreCase("X")), the entire board prints 0s. I don't understand why.

    EDIT* Also I realize that the addMine will cause a ArrayIndexOutOfBoundsException, but I can't even get the method to run down that far into itself, so I'll do that grunt work once I figure out how I can even get it to clear.

    EDIT* Okay, I fixed the addMine ArrayIndexOutOfBoundsException. I actually just copied the code from the constructor method into it, though I might just make that whole chunk a method on its own.
    Last edited by Faytrow; 01-23-2013 at 12:12 PM.

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Issue with reading from a file with a scanner and incrementing an int[][]

    It might be helpful to read up on the Scanner documentation, specifically what nextLine() returns and what you're trying to compare that to. Also remember that once the scanner reads from the stream, it throws that part away and doesn't read it again. So if you're doing:
    Java Code:
    if (... input.nextLine() ...){}
    else if (... input.nextLine() ...){}
    you've actually read two different lines.

  3. #3
    Faytrow is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Issue with reading from a file with a scanner and incrementing an int[][]

    Quote Originally Posted by Shoss View Post
    It might be helpful to read up on the Scanner documentation, specifically what nextLine() returns and what you're trying to compare that to. Also remember that once the scanner reads from the stream, it throws that part away and doesn't read it again. So if you're doing:
    Java Code:
    if (... input.nextLine() ...){}
    else if (... input.nextLine() ...){}
    you've actually read two different lines.
    Does this also happen with .hasNextLine? So if I have
    Java Code:
    if(input.hasNextLine() && input.nextLine()...)
    does that make it so I read two lines?

  4. #4
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Issue with reading from a file with a scanner and incrementing an int[][]

    What does the documentation tell you . (Hint: The third sentence.)

  5. #5
    Faytrow is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Issue with reading from a file with a scanner and incrementing an int[][]

    No it does not, okay. But then why does the if-else statement print out only -1s? No matter how I try to rewrite it it either only sets -1s, or it only sets 0s.

    EDIT* by "No it does not", I mean "no, .hasNextLine() does not cause me to skip past any lines."
    Last edited by Faytrow; 01-23-2013 at 05:14 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Issue with reading from a file with a scanner and incrementing an int[][]

    Currently you are reading an entire line and comparing it with a '.'.
    Since no line in the input file contains only a '.' it will always fail the test.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Faytrow is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Issue with reading from a file with a scanner and incrementing an int[][]

    But there are spaces between the '.'s and the 'X's. Shouldn't the white space trigger the .nextLine() to conclude.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Issue with reading from a file with a scanner and incrementing an int[][]

    The clue is in the name.
    nextLine uses line separators.
    next() uses whitespace.
    And you would need to use hasNext() as well.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Faytrow is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Issue with reading from a file with a scanner and incrementing an int[][]

    Quote Originally Posted by Tolls View Post
    The clue is in the name.
    nextLine uses line separators.
    next() uses whitespace.
    And you would need to use hasNext() as well.
    *Facepalm* thank you SO MUCH! =D

Similar Threads

  1. Scanner problem when reading file
    By Gatsu in forum New To Java
    Replies: 8
    Last Post: 12-24-2012, 01:56 PM
  2. Scanner malfunction - Reading a file
    By Noceo in forum New To Java
    Replies: 10
    Last Post: 10-30-2012, 06:49 PM
  3. Reading a file with Scanner
    By Games2Design in forum New To Java
    Replies: 5
    Last Post: 10-04-2012, 10:10 AM
  4. File reading with Scanner
    By mik in forum New To Java
    Replies: 2
    Last Post: 08-31-2012, 09:44 AM
  5. Scanner not reading every character in a file?
    By Brandonhspace in forum New To Java
    Replies: 5
    Last Post: 06-07-2012, 07:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •