当用户将命令行参数留空时如何正确显示错误?
How to properly display an error when Commandline Argument is left blank by user?
所以你可以看到我正在从一个文件中读取并显示文件中的所有整数并将数量放入一个数组中,我需要帮助的只是一个打印出的 trycatch 块 "You did not enter anything", 基本上当命令行参数被用户留空时。
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
public class Print5{
public static void main(String[] commandlineArgument) {
Integer[] array = Print5.readFileReturnIntegers(commandlineArgument[0]);
Print5.printArrayAndIntegerCount(array, commandlineArgument[0]);
}
public static Integer[] readFileReturnIntegers(String filename) {
Integer[] temp = new Integer[10000];
int i = 0;
File file = new File(filename);//Connects File
Scanner inputFile = null;
try{
inputFile = new Scanner(file);
}
catch(FileNotFoundException Exception1) {
System.out.println("File not found!"); //error message when mistyped
}
//where the blank error arg will go
if (inputFile != null) {
try {
while (inputFile.hasNext()) {
try {
temp[i] = inputFile.nextInt();
i++;
} catch (InputMismatchException Exception3) { //change this back to e if doesnt work
inputFile.next();
}
}
}
finally {
inputFile.close();
}
Integer[] array = new Integer[i];
System.arraycopy(temp, 0, array, 0, i);
return array;
}
return new Integer[] {};
}
//Prints the array
public static void printArrayAndIntegerCount(Integer[] array, String filename) {
System.out.println("number of integers in file \"" + filename + "\" = " + array.length);
for (int i = 0; i < array.length; i++) {
System.out.println("index = " + i + "," + " element = " + array[i]);
}
}
}
添加检查数组大小并在显示错误信息后退出程序:
public static void main(String[] commandlineArgument) {
if(commandlineArgument.length < 1) {
System.err.println("Your error message"); // use the std error stream
System.exit(-1);
}
...
按照惯例,System.exit()
的非零状态参数表示异常终止。
只需检查 args 数组长度:
public static void main(String[] commandlineArgument) {
if ( commandlineArgument.length > 0 ) {
Integer[] array = Print5.readFileReturnIntegers(commandlineArgument[0]);
Print5.printArrayAndIntegerCount(array, commandlineArgument[0]);
}
else {
System.out.println("usage - ... your message");
}
}
尝试使用 Apache CLI 库。
http://commons.apache.org/proper/commons-cli/
它允许您定义必需的选项并在缺少时失败。
所以你可以看到我正在从一个文件中读取并显示文件中的所有整数并将数量放入一个数组中,我需要帮助的只是一个打印出的 trycatch 块 "You did not enter anything", 基本上当命令行参数被用户留空时。
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
public class Print5{
public static void main(String[] commandlineArgument) {
Integer[] array = Print5.readFileReturnIntegers(commandlineArgument[0]);
Print5.printArrayAndIntegerCount(array, commandlineArgument[0]);
}
public static Integer[] readFileReturnIntegers(String filename) {
Integer[] temp = new Integer[10000];
int i = 0;
File file = new File(filename);//Connects File
Scanner inputFile = null;
try{
inputFile = new Scanner(file);
}
catch(FileNotFoundException Exception1) {
System.out.println("File not found!"); //error message when mistyped
}
//where the blank error arg will go
if (inputFile != null) {
try {
while (inputFile.hasNext()) {
try {
temp[i] = inputFile.nextInt();
i++;
} catch (InputMismatchException Exception3) { //change this back to e if doesnt work
inputFile.next();
}
}
}
finally {
inputFile.close();
}
Integer[] array = new Integer[i];
System.arraycopy(temp, 0, array, 0, i);
return array;
}
return new Integer[] {};
}
//Prints the array
public static void printArrayAndIntegerCount(Integer[] array, String filename) {
System.out.println("number of integers in file \"" + filename + "\" = " + array.length);
for (int i = 0; i < array.length; i++) {
System.out.println("index = " + i + "," + " element = " + array[i]);
}
}
}
添加检查数组大小并在显示错误信息后退出程序:
public static void main(String[] commandlineArgument) {
if(commandlineArgument.length < 1) {
System.err.println("Your error message"); // use the std error stream
System.exit(-1);
}
...
按照惯例,System.exit()
的非零状态参数表示异常终止。
只需检查 args 数组长度:
public static void main(String[] commandlineArgument) {
if ( commandlineArgument.length > 0 ) {
Integer[] array = Print5.readFileReturnIntegers(commandlineArgument[0]);
Print5.printArrayAndIntegerCount(array, commandlineArgument[0]);
}
else {
System.out.println("usage - ... your message");
}
}
尝试使用 Apache CLI 库。 http://commons.apache.org/proper/commons-cli/
它允许您定义必需的选项并在缺少时失败。