We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,924 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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

    }
2
Contributors
2
Replies
9 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
RM@Bowdoin
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

~s.o.s~
Failure as a human
Administrator
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 55

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...

~s.o.s~
Failure as a human
Administrator
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 55

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0637 seconds using 2.72MB