请求文件名作为输入--java
问题描述:
我此刻代码:请求文件名作为输入--java
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));
}
}
可以看出,这个程序的工作原理是从file.txt的文件抓取输入。然而,我怎么修改这个代码以便请求要求每次程序运行时都有一个文件名(可能在命令行),并且使用那作为输入? 我想添加一个字符串作为参数,然后将该字符串更改为文件名。但说起来容易做起来难。建议?
答
你可以把它作为一个参数和修改代码 -
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;
}
答
您可以使用此代码:
public static boolean[][] readOpenFromFile(String file) {
final File f = new File(file);
// same code ...
}
public static void main(String[] args) {
if(args != null && args.length == 1) {
String file = args[0];
final boolean[][] open = readOpenFromFile(file);
print(flow(open));
System.out.println(percolates(open));
}
}
+0
谢谢!我如何定义main的第二个参数,例如通过要求一些char myChar,如果不是args [1]? –
答
你需要这样的事情,但你必须添加检查文件名:
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
while (!(CurLine.equals("quit"))){
CurLine = in.readLine();
if (!(CurLine.equals("quit"))){
System.out.println("You typed: " + CurLine);
}
}
谢谢!如果我要向main方法添加另一个参数char,它会变成args [1]吗?不清楚这是如何工作的... –
args [1]给我一个'不兼容的类型'错误。 我将如何去添加第二个参数到主要方法? –
你怎么传递和使用第二个参数? – Jayendra