I implore you to ignore the poor formatting, ignore the overall functionality even; my problem lies with trying to call the Scanner scan within flow method from readBoolean and readBoolean2D methods.
It says it cannot find variable scan. How can I access this variable?

import java.util.*;
    import java.io.*;


    public class VerticalPercolation {
        // given an N-by-N matrix of open sites, return an N-by-N matrix
        // of sites reachable from the top via a vertical path of open sites
        public static boolean[][] flow (final boolean[][] open) {
            int m = 0;
            int n = 0;
            final File f = new File("file.txt");
            Scanner scan = null;
            try {
                scan = new Scanner(f);
            }
            catch(FileNotFoundException ex) {
                System.exit(0);
            }

            final boolean[][] full = new boolean[m][n];
            // identify full sites in row 0
            for (int j = 0; j < n; j++) {
                full[0][j] = open[0][j];
            }
    
            // update remaining rows
            for (int i = 1; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    full[i][j] = open[i][j] && full[i - 1][j];
                }
            }
    
            return full;
        }
    
        // does the system percolate?
        public static boolean percolates (final boolean[][] open) {
            final int n = open.length;
            final boolean[][] full = flow (open);
            for (int j = 0; j < n; j++) {
                if (full[n - 1][j]) {
                    return true;
                }
            }
            return false;
        }

        // return a random N-by-N boolean matrix, where each entry is
        // true with probability p
        public static boolean[][] random (final int n, final double p, final Random rnd) {
            final boolean[][] a = new boolean[n][n];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    a[i][j] = rnd.nextDouble () < p;
                }
            }
            return a;
        }
    
        public static boolean readBoolean () {
            final String s = scan.next ();
        
            if (s.equalsIgnoreCase ("true")) {
                return true;
            }
            if (s.equalsIgnoreCase ("false")) {
                return false;
            }
            if (s.equals ("1")) {
                return true;
            }
            if (s.equals ("0")) {
                return false;
            }
            throw new java.util.InputMismatchException ();
        }

        public static boolean[][] readBoolean2D () {
            final int m = scan.nextInt ();
            final int n = scan.nextInt ();
            final boolean[][] a = new boolean[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    a[i][j] = readBoolean ();
                }
            }
            return a;
        }

        public static void print (final boolean[][] a) {
            final int m = a.length;
            final int n = a[0].length;
            System.out.println (m + " " + n);
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (a[i][j]) {
                        System.out.print ("1 ");
                    } else {
                        System.out.print ("0 ");
                    }
                }
                System.out.println ();
            }
        }

        //main method
        public static void main (final String[] args) {

            final boolean[][] open = readBoolean2D ();
            print (flow (open));
            System.out.println (percolates (open));
        }

    }

Recommended Answers

All 2 Replies

Make `scan` a private static variable and initialize it in the main() method. In that case, you'd be able to use `scan` in all your static methods. In case you decide to clean up stuff, just remove the static modifier from the scan declaration and all the methods and it should still work assuming you somehow end up initializing scan before accessing it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.