Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Reading and Writing 2d boolean array

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Reading and Writing 2d boolean array

    I have a file that will be in this format:

    12
    1 0 0
    0 1 0
    1 0 1

    where the first line is the count

    and after is the boolean array content

    For reading the array, i count the first line then initialize the N by N boolean array, and then iterate through each line and initialize the boolean array row by row.
    However, my reading function wont work as it keeps saying that the boolean array grid wasn't initialized?

    the error says:
    Error53, 48) java: variable grid might not have been initialized




     
    static void getN(String fileName) throws FileNotFoundException, IOException
        {
            int generation = 0;
            int gridSize = 0;
            BufferedReader buffReader = new BufferedReader(new FileReader("/Users/alkaitoob/IdeaProjects/untitled9/src/test"));
            buffReader.readLine();
            while(buffReader.readLine()!=null)
            {
                gridSize++;
            }
            buffReader.close();
            boolean [][] grid = new boolean[gridSize][gridSize];
     
            Scanner in = new Scanner(new File("/Users/alkaitoob/IdeaProjects/untitled9/src/test"));
            gridSize=in.nextInt();
            //in.nextLine();
            for(int r= 0 ; r<gridSize;r++)
            {
                for(int c= 0;c<gridSize;c++)
                {
                    if(in.nextInt()==1)
                    {
                        grid[r][c] = true;
                    }
                    else {
                        grid[r][c] = false;
                    }
                }
            }
            for(int i=0; i<gridSize; i++)
            {
                for(int j=0; j<gridSize; j++)
                {
                    System.out.print(" " + grid[i][j]);
                }
                System.out.println("\n");
            }
            in.close();
        }
     
    }
    Last edited by Kieve; December 2nd, 2019 at 08:41 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading and Writing 2d boolean array

    There are two sections of code posted. Which one has the error?
    Please copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2019
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading and Writing 2d boolean array

    Thanks done

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading and Writing 2d boolean array

    What source code line is that error message referring to? The posted source does not have all the source code's lines. Where is line 53?

    The compiler thinks It is possible for an exception to throw the execution out of the try{} catch block before grid is assigned a value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Kieve (December 2nd, 2019)

  6. #5
    Junior Member
    Join Date
    Dec 2019
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading and Writing 2d boolean array

    thanks fixed my code now it works properly

Similar Threads

  1. File reading/writing
    By Masic1990 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 10th, 2014, 02:02 AM
  2. Reading and writing file
    By FaisalBahadur in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 12th, 2014, 05:38 PM
  3. Boolean writing and reading
    By wuppy29 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 13th, 2012, 02:21 PM
  4. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 08:41 PM
  5. file reading & writing
    By macko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 12th, 2011, 08:54 AM