Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6643929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:06:28+00:00 2026-05-26T00:06:28+00:00

My code at the moment: import java.util.*; import java.io.*; public class Percolation { //

  • 0

My code at the moment:

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

public class Percolation {
    // 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
    private static Scanner scan = null;

    public static boolean[][] readOpenFromFile() {
        final File f = new File("file.txt");
        try {
            scan = new Scanner(f);
        }
        catch(FileNotFoundException ex) {
            System.exit(0);
        }

        final int m = scan.nextInt();
        final int n = scan.nextInt();
        final boolean[][] grid = new boolean[m][n];

        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                grid[i][j] = readBoolean();
            }
        }
        return grid;
    }

    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[][] flow(final boolean[][] open) {
        final int n = open.length;
        final boolean[][] full = new boolean[n][n];
        for(int j = 0; j < n; j++) {
            flow(open, full, 0, j);
        }
        return full;
    }

    public static void flow(final boolean[][] open, final boolean[][] full, final int i, final int j) {
        final int n = open.length;

        // base cases
        if(( i < 0) ||( i >= n)) {
            return; // invalid row
        }
        if(( j < 0) ||( j >= n)) {
            return; // invalid column
        }
        if(!open[i][j]) {
            return; // not an open site
        }
        if(full[i][j]) {
            return; // already marked as full
        }

        // mark i-j as full
        full[i][j] = true;

        flow(open, full, i + 1, j); // down
        flow(open, full, i, j + 1); // right
        flow(open, full, i, j - 1); // left
        flow(open, full, i - 1, j); // up
    }

    // 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]) {
                System.out.println("percolates");
                return true;
            }
        }
        System.out.println("does not percolate");
        return false;
    }

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

    public static void main(String[] args) {
        final boolean[][] open = readOpenFromFile();
        print(flow(open));
        System.out.println(percolates(open));
    }
}

It can be seen that this program works by grabbing input from the file.txt file. However, how could I modify this code so as to request require a file name (perhaps at the command-line) each time the program is run, and use that as input?
I would think to add a String as an argument, then change that String into a file name. But this is easier said than done. Suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:06:29+00:00Added an answer on May 26, 2026 at 12:06 am

    You can take it as an argument and modify code to –

    public static void main(String[] args) {
        // args[0] - Full path of the file
        final boolean[][] open = readOpenFromFile(args[0]);
        print(flow(open));
        System.out.println(percolates(open));
    }
    
    public static boolean[][] readOpenFromFile(String filepath) {
        final File f = new File(filepath);
        try {
            scan = new Scanner(f);
        }
        catch(FileNotFoundException ex) {
            System.exit(0);
        }
    
        final int m = scan.nextInt();
        final int n = scan.nextInt();
        final boolean[][] grid = new boolean[m][n];
    
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                grid[i][j] = readBoolean();
            }
        }
        return grid;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie Scala Question: Say I want to do this [Java code] in Scala: public
package timeToys; import java.util.regex.Pattern; ** * A DayTime is an immutable object that stores
At the moment my code (PHP) has too many SQL queries in it. eg...
At the moment my code looks like this: # Assign values for saving to
I am using in my code at the moment a ReentrantReadWriteLock to synchronize access
I'm using the STL map data structure, and at the moment my code first
At the moment I have the following code which works fine. label = new
Take this example code (ignore it being horribly inefficient for the moment) let listToString
The following code summarizes the problem I have at the moment. My current execution
at the moment I'm really interested in expression templates and want to code a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.