Another way to do this so it works for any number of arguments is to
use a try-catch block.
int[] ages=new int[args.length/2];//create array ages with half the
number of values of the entire argument (as we have names and ages)
String[] names=new String[args.length/2];//also create an
array for the names in the same manner
int g=0;
int h=0;
for(int i=0;i<args.length;i++){//repeat the block until
i=args.length (i.e. its been through the entire argument)
try{//use a try to see if the array element can be parsed
as an integer
Integer.parseInt(args[i]);
} catch (NumberFormatException e) {//if it can't, catch
the NumberFormatException and add the String to the names array
names[g]=args[i];
g++;
continue;//go back to the start
}
ages[h]=Integer.parseInt(args[i]);//if there is no error,
its not caught and gets put into the ages array instead
h++;
}
Bit more flexible than some of the other suggestions here =o)
James Kendall
On Jul 30, 5:18 pm, pedro oliveira <[email protected]> wrote:
> Hi,
> I have some questions regarding the Homework exercise of Lab - 1038.
>
> 1 - Regarding numbers of ages, is it mandatory for the program to check if
> the ages passed as command-line arguments are between 3 and 6, or is a
> programmer's choice?
>
> Because that is a problem when making the average, if the number is
> between 3 and 6, i haven't figure out a way to make the average equation
>
> 2 - I can't properly understand how to recieve the arguments in the form
> "name then ages", and only recieve the parameter ages to make the average.
>
> Any help would be largely apreciated.
>
> Thank you.
>
> Pedro Oliveira
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---