-4

So if a user enters age as fishs instead of a integer a InputMismatchException prints "Error in the input enter a number". or if the user does not enter anything in cmd (command line) than a ArrayIndexOutOfBoundsException prints "Not enough arguments on the command line"

Fuuthermore No scanner should be used. And age = Integer.parseInt( args [0]) ; must be used to convert age from character to integer

Modify your program in Question to take the age of the user from the command line as the program runs. Your program should handle problems if the user does not input a number on the command line or makes an error in input.

Here is the code:

 public class age {

public static void main(String args[]){
    int age = 0;




     // try   
    try{
       // convert age from character to integer 
       age = Integer.parseInt( args [0]) ;

       // check input age value
       // if ueser inputs age less or equal to 12
        if(age<= 12)
        System.out.println("You are very young");
        // if user enters age larger than 12 and less than 20
         if(age > 12 && Age < 20)
            System.out.println("You are a teen");
        // if user enters age larger than 20
       if (age > 20)
            System.out.println("wow" +Age+" is very old");
    }
    catch(InputMismatchException e){
       System.out.println("Error in the input enter a number");
    }
 catch ( ArrayIndexOutOfBoundsException exception0) {System.out.println   ("Not enough arguments on the command line" ) ; }
  }   
}   
bob
  • 1
  • 4
  • https://stackoverflow.com/questions/5488072/reading-in-from-system-in-java This is duplicate... – Wulf Apr 24 '18 at 05:28
  • 2
    Possible duplicate of [Reading in from System.in - Java](https://stackoverflow.com/questions/5488072/reading-in-from-system-in-java) – Piro Apr 24 '18 at 05:31
  • When you want the first argument you should use `args[0]`. Also you have to execute the program like `java Age 42` when you want to pass the age 42 to the program. –  Apr 24 '18 at 05:38
  • @Wufo and @Piro: This is not a duplicate of the question [Reading in from System.in - Java](https://stackoverflow.com/q/5488072/8097737). Since the question is to read the arguments and not from `System.in`. –  Apr 24 '18 at 05:42
  • @devpuh he is already reading age from args[2], clearly question needs better explanation in that case. – Piro Apr 24 '18 at 05:54
  • Please follow the Java Naming Conventions – variable names (other than marked `static final`) should always start with lowercase. – MC Emperor Apr 24 '18 at 06:25
  • @bob Please try to describe what your program should do and what unexpected output you currently get. Also read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) –  Apr 24 '18 at 06:56
  • i fixed the question up. – bob Apr 25 '18 at 07:35
  • So if a user enters age as fishs instead of a integer a InputMismatchException prints "Error in the input enter a number". or if the user does not enter anything in cmd (command line) than a ArrayIndexOutOfBoundsException prints "Not enough arguments on the command line" Futhermore No scanner should be used. And age = Integer.parseInt( args [0]) ; must be used to convert age from character to integer – bob Apr 25 '18 at 07:36
  • InputMismatchException is not working with Integer.parseInt( args [0]) ; than how can you get the exception to work, without a scanner – bob Apr 25 '18 at 07:36

1 Answers1

0

So you want to catch the exception that is thrown when the input is not a number. Since you are using Integer.parseInt(String s) the thrown excetion would be NumberFormatException. (See the JavaDoc for Integer.parseInt(String s))

Note: You should follow the java naming convention

You code should look like:

public class Age {
    public static void main(String args[]) {
        int age = 0;

        try {
            // convert age from character to integer 
            age = Integer.parseInt(args[0]) ;
            // check input age value
            // if ueser inputs age less or equal to 12
            if(age <= 12)
                System.out.println("You are very young");
            // if user enters age larger than 12 and less than 20
            if(age > 12 && age < 20)
                System.out.println("You are a teen");
            // if user enters age larger than 20
            if (age > 20)
                System.out.println("wow " + age + " is very old");
        } catch (NumberFormatException e){
            System.out.println("Error in the input enter a number");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Not enough arguments on the command line" ); 
        }
    }
}

But this code still could behave unexpected when the user enters 20 since the program didn't output anything in this case. To fix this you should change

// if user enters age larger than 20
if (age > 20)

into

// if user enters age at least 20
if (age >= 20)