1,105,242 Community Members

Call Scanner within Try{} from another method -- Java

Member Avatar
RM@Bowdoin
Newbie Poster
2 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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));
        }

    }
Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

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.

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

And please, let us know if you have posted the question somewhere else. It seems that the question posted here: http://stackoverflow.com/questions/7657228/call-scanner-within-try-from-another-method-java already has good replies...

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: