Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Looking to print names and age from Command line arguments

$
0
0
I am writing a program that will allow me to enter between 2 and 6 names and age as command line arguments.
I want an error if the arguments is not between 2 nd 6 and an error if a name is entered and the age was not.
I then print the average age.

I got most of it working except I am not able to figure out how to determine if the name was entered and the age was not. I am not liking my first for loop. Is there a better way to write this?
Thanks
here is my code:

public class Arguments {

    /**
     * @param args
     */
    public static void main(String[] args) {
      
      
      int length = args.length;
      if((length <= 3 || length >=14)){
            System.out.println("Need more arguments");
      }       // Display the arguments from the command line
        for(int counter = 0; counter < args.length; counter++) {
           System.out.println(args[counter]);
            
        }
        
              double num = 0;
             for(int i =1; i < args.length; i +=2){
                 num += Integer.parseInt(args[i]);
             
              }
              System.out.println( "The average age is =" + num / (args.length /2));
        }
        
        

}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>