簡體   English   中英

Java程序從命令行獲取輸入,並進行異常處理

[英]Java program taking input from the command line, with exception handling

再次需要幫助。 該程序將把輸入作為年齡,並根據輸入拋出異常。 在程序運行時,它將花費用戶的年齡,如果用戶未在命令行上輸入數字或輸入錯誤,則程序應處理問題。

我為此的代碼是:

 public class Age2 {

    public static void main(String args[]){

        try{
            int age = Integer.parseInt(args[0]);  //taking in an integer input throws NumberFormat Exception if not an integer

            if(age<= 12)
               System.out.println("You are very young");

            else if(age > 12 && age <= 20)
               System.out.println("You are a teenager");

            else
                System.out.println("WOW "+age+" is old");
        }

        catch(NumberFormatException e){   //is input is not an integer, occurs while parsing the command line input argument
            System.out.println("Your input is not a number");

        }catch(ArrayIndexOutOfBoundsException e){ //as takes in input from command line which is stored to a Args array in main, if this array is null implies no input given
            System.out.println("Please enter a number on the command line");
        }

    }   
}//end class

我的輸出:

輸出量

但是,如果用戶犯任何錯誤,該程序也應顯示異常:

“ 22 22”或“ 3 kjhk”

見下圖:

沒有顯示任何錯誤。

您能幫我修改一下嗎? 謝謝大家

如果傳遞22 22kjj ,則args將具有兩個元素: 2222kjj

您可以添加一個條件: if(args.length != 1) System.out.println("only one number");

當使用

Java Age2 22 22kjj

您將獲得“ 22”和“ 22kjj”作為程序參數數組的單獨成員:

  1. args[0]包含“ 22”
  2. args[1]包含“ 22kjj”

由於只檢查了args[0] ,因此,對於args[1]格式錯誤,不會有任何問題。 也許您還想檢查參數數組的長度:

if (args.length != 1) {
    System.out.println("Please enter exactly one number!");
}

用調用程序

java Age2 "22 22kjj"

或搭配

java Age2 "22kjj"

將為您提供所需的輸出。

當您引用args[0] ,該程序仍將正常運行,並且從命令行給出的args由空格分隔,因此即使添加其他參數也始終為22。

您可以簡單地驗證年齡是唯一給定的參數。

if(args.length > 1)
   throw new Exception();

之所以起作用,是因為args[0]忽略第一個參數。 您要做的就是檢查args[1]

例如

public class Age2 {

public static void main(String args[]){
if(args.length == 1)
{
    try{
        int age = Integer.parseInt(args[0]);  //taking in an integer input throws NumberFormat Exception if not an integer

        if(age<= 12)
           System.out.println("You are very young");

        else if(age > 12 && age <= 20)
           System.out.println("You are a teenager");

        else
            System.out.println("WOW "+age+" is old");
    }

    catch(NumberFormatException e){   //is input is not an integer, occurs while parsing the command line input argument
        System.out.println("Your input is not a number");

    }catch(ArrayIndexOutOfBoundsException e){ //as takes in input from command line which is stored to a Args array in main, if this array is null implies no input given
        System.out.println("Please enter a number on the command line");
    }


 }else{
   System.out.println("Pls give a single Number");
  }
}//end class

你必須做

catch(exception e){
  e.printstacktrace // or something like that to get your exception
  System.out.println("thats not a number")}

您現在擁有它的方式就是告訴程序如果有異常,則輸出您的字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM