在 java 中,如何处理有关 Windows 命令行参数的异常?
In java, how can I handle exceptions regarding Windows command line arguments?
我浏览了一堆其他问题和在线,但找不到任何具体回答这个问题的内容。我正在尝试根据 Windows 命令行参数处理和确定有效输入。作为示例,我只是想尽可能简单地查看输入的数字是否为正数。最大的问题,也是让我无法找到具体答案的原因,是我真的在尝试使用递归让它一直询问直到输入有效输入,而不是仅仅终止程序。
Algorithm:
If there is an argument provided and it's a positive integer
Display value
Otherwise
Until the argument is a positive number
Prompt for a positive integer
Display value
我与代码搏斗并最终让它工作,但它看起来确实效率低下,重复并且被黑客攻击在一起。起初,我在捕获的异常中有 while 循环,但这允许其他东西从命令行中溜走。我怎样才能使它尽可能高效并防止任何逻辑错误或异常?解决这个问题时,我的算法应该采用什么方法?这是我的代码:
import java.util.Scanner;
public class Test
{
public static void main( String[] args )
{
String arg;
Scanner user_input = new Scanner(System.in);
int i = 0;
try {
arg = args[0];
i = Integer.parseInt(arg);
} catch( ArrayIndexOutOfBoundsException e ) {
arg = "";
} catch( NumberFormatException e2 ) {
arg = "";
}
while( i <= 0 )
{
System.out.print("Please type in a positive whole number. ");
arg = user_input.next();
try {
i = Integer.parseInt(arg);
} catch( NumberFormatException e2 ) {
System.out.print("That's a letter! ");
continue;
}
if( i <= 0 )
{
System.out.print("That's a negative. ");
}
}
System.out.println("Input is " + i);
}
}
我修改了你的代码,让它按照你想要的方式运行。试试这个:
public static void main( String[] args ) {
String arg;
Scanner user_input = new Scanner(System.in);
int numTries = 0, value = 0;
try {
numTries = Integer.parseInt(args[0]); // get max number of tries
} catch (ArrayIndexOutOfBoundsException e) {
arg = "";
} catch (NumberFormatException e2) {
arg = "";
}
// try 'numTries' times to read a valid number input
for (int i=numTries; i > 0; --i) {
System.out.print("Please type in a positive whole number: ");
arg = user_input.next();
try {
value = Integer.parseInt(arg);
} catch(NumberFormatException e2) {
System.out.println("That's a letter! ");
continue;
}
if (value <= 0) {
System.out.println("That's a negative. ");
}
break; // exit loop if number input found
}
System.out.println("Input is " + value);
}
此代码已在 IntelliJ 上测试,运行没有问题。
试试这个:
代码很长,这是因为需要两个单独的try块;一个用于命令行参数,另一个用于通过扫描器提供的参数...
我必须创建自己的自定义异常,"NegativeNumberException"...
import java.util.Scanner;
public class NegativeNumberException extends Exception{
NegativeNumberException(){
System.out.println(exceptionMessage);
}
String exceptionMessage = "Number must be positive";
static int num;
public static void main(String[] args) throws NegativeNumberException{
try
{
if(Integer.parseInt(args[0])<0){
throw new NegativeNumberException();
}
else{
int num = Integer.parseInt(args[0]);
System.out.println("Your number is: " + num);
}
}
catch(NumberFormatException ex){
System.out.println("That's not even a number.");
}
catch(NegativeNumberException ex){
ex.getMessage();
}
while(num==0){
try{
System.out.println("Enter a positive number:");
Scanner input = new Scanner(System.in);
int num1 = input.nextInt();
if(num1<0){
throw new NegativeNumberException();
}
num = num1;
break;
}catch(Exception ex){
System.out.println("Positive number only, try again...");
}
}//End While
System.out.println("Your number is:" + num);
}
}
输入:(命令行):哈哈
输出
(Console):That's not even a number
Enter a positive int
(Console input via Scanner): -4
(Console):Number must be positive
Positive number only, try again...
Enter a positive number:
(Console input via Scanner): 3
(Console):Your number is: 3
我浏览了一堆其他问题和在线,但找不到任何具体回答这个问题的内容。我正在尝试根据 Windows 命令行参数处理和确定有效输入。作为示例,我只是想尽可能简单地查看输入的数字是否为正数。最大的问题,也是让我无法找到具体答案的原因,是我真的在尝试使用递归让它一直询问直到输入有效输入,而不是仅仅终止程序。
Algorithm:
If there is an argument provided and it's a positive integer
Display value
Otherwise
Until the argument is a positive number
Prompt for a positive integer
Display value
我与代码搏斗并最终让它工作,但它看起来确实效率低下,重复并且被黑客攻击在一起。起初,我在捕获的异常中有 while 循环,但这允许其他东西从命令行中溜走。我怎样才能使它尽可能高效并防止任何逻辑错误或异常?解决这个问题时,我的算法应该采用什么方法?这是我的代码:
import java.util.Scanner;
public class Test
{
public static void main( String[] args )
{
String arg;
Scanner user_input = new Scanner(System.in);
int i = 0;
try {
arg = args[0];
i = Integer.parseInt(arg);
} catch( ArrayIndexOutOfBoundsException e ) {
arg = "";
} catch( NumberFormatException e2 ) {
arg = "";
}
while( i <= 0 )
{
System.out.print("Please type in a positive whole number. ");
arg = user_input.next();
try {
i = Integer.parseInt(arg);
} catch( NumberFormatException e2 ) {
System.out.print("That's a letter! ");
continue;
}
if( i <= 0 )
{
System.out.print("That's a negative. ");
}
}
System.out.println("Input is " + i);
}
}
我修改了你的代码,让它按照你想要的方式运行。试试这个:
public static void main( String[] args ) {
String arg;
Scanner user_input = new Scanner(System.in);
int numTries = 0, value = 0;
try {
numTries = Integer.parseInt(args[0]); // get max number of tries
} catch (ArrayIndexOutOfBoundsException e) {
arg = "";
} catch (NumberFormatException e2) {
arg = "";
}
// try 'numTries' times to read a valid number input
for (int i=numTries; i > 0; --i) {
System.out.print("Please type in a positive whole number: ");
arg = user_input.next();
try {
value = Integer.parseInt(arg);
} catch(NumberFormatException e2) {
System.out.println("That's a letter! ");
continue;
}
if (value <= 0) {
System.out.println("That's a negative. ");
}
break; // exit loop if number input found
}
System.out.println("Input is " + value);
}
此代码已在 IntelliJ 上测试,运行没有问题。
试试这个:
代码很长,这是因为需要两个单独的try块;一个用于命令行参数,另一个用于通过扫描器提供的参数...
我必须创建自己的自定义异常,"NegativeNumberException"...
import java.util.Scanner;
public class NegativeNumberException extends Exception{
NegativeNumberException(){
System.out.println(exceptionMessage);
}
String exceptionMessage = "Number must be positive";
static int num;
public static void main(String[] args) throws NegativeNumberException{
try
{
if(Integer.parseInt(args[0])<0){
throw new NegativeNumberException();
}
else{
int num = Integer.parseInt(args[0]);
System.out.println("Your number is: " + num);
}
}
catch(NumberFormatException ex){
System.out.println("That's not even a number.");
}
catch(NegativeNumberException ex){
ex.getMessage();
}
while(num==0){
try{
System.out.println("Enter a positive number:");
Scanner input = new Scanner(System.in);
int num1 = input.nextInt();
if(num1<0){
throw new NegativeNumberException();
}
num = num1;
break;
}catch(Exception ex){
System.out.println("Positive number only, try again...");
}
}//End While
System.out.println("Your number is:" + num);
}
}
输入:(命令行):哈哈
输出
(Console):That's not even a number
Enter a positive int
(Console input via Scanner): -4
(Console):Number must be positive
Positive number only, try again...
Enter a positive number:
(Console input via Scanner): 3
(Console):Your number is: 3