我正在研究一些采用输入文件和格式化文本的代码。我只发布了我的主要代码。我试图找到最大行长度,这是我的else语句开头的代码,它编译并运行完美,但不打印出“宽度”,似乎我有某种语法问题或我似乎无法弄明白的东西。我有几个人看了我的代码,但没有成功,这里有没有人有任何建议?
public static void main (String[] args) {
int argix = 0;
if (args.length == 0) {
//There are no filenames given on the command line.
out.printf ("FILE: -%n");
format (new Scanner (in));
}else {
// Iterate over each filename given on the command line.
if(args[0].matches("-\\d+")){
width=Integer.parseInt("-\\d+");
width=width*-1;
System.out.println(width);
argix = 1;
}
for (; argix < args.length; ++argix) {
String filename = args[argix];
if (filename.equals ("-")) {
// Treat a filename of "-" to mean System.in.
out.printf ("FILE: -%n");
format (new Scanner (in));
}
else {
//Open the file and read it, or error out.
try {
Scanner infile = new Scanner (new File (filename));
out.printf ("FILE: %s%n", filename);
format (infile);
infile.close();
}catch (IOException error) {
exit_status = EXIT_FAILURE;
err.printf ("%s: %s%n", JAR_NAME,
error.getMessage());
}
}
}
}
exit (exit_status);
}