BlueDwarf
BlueDwarf

Reputation: 83

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

need help again. The program is about taking input as age and throwing exception according to the input. It will take the age of the user from the command line as the program runs, the program should handle problems if the user does not input a number on the command line or makes an error in input.

The code I have for this is:

 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

The output I'm having:

Output

But the program also should show exception if the user makes any mistake like:

"22 22" or "3 kjhk"

see picture below:

Not showing any error....

Could you please help me to modify this? Thanks all.

Upvotes: 1

Views: 12317

Answers (5)

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37604

The reason why it's working is, because args[0] delievers the first argument. What you have to do is check for args[1].

e.g.

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

Upvotes: 1

Seelenvirtuose
Seelenvirtuose

Reputation: 20618

When calling a program with

java Age2 22 22kjj

you will get the "22" and the "22kjj" as individual members of the program arguments' array:

  1. args[0] contains "22"
  2. args[1] contains "22kjj"

As you only checked for args[0], you will not get any problem with a misformed args[1]. Maybe you want to also check the length of the arguments' array:

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

Calling your program with

java Age2 "22 22kjj"

or with

java Age2 "22kjj"

will give you the desired output.

Upvotes: 3

assylias
assylias

Reputation: 328649

If you pass 22 22kjj, args will have two elements: 22 and 22kjj.

You could add a condition: if(args.length != 1) System.out.println("only one number");

Upvotes: 4

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

The program will still function properly as you refer to args[0] and args given from command line are separated by spaces, hence always 22 even if you add other arguments.

You could simply validate that the age is the only given arguments.

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

Upvotes: 1

GTR SEVER
GTR SEVER

Reputation: 15

you have to do

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

the way that you have it now is just telling the program to output your string if there is an exception.

Upvotes: -2

Related Questions