Pedro,
Lets say you want 4 names and ages in the format name1 age1 name2 age2 name3
age3 name4 age4, you could pass them as arguments into your code ... see
below. This code does the folllowing:
verifies if the total number of arguments passed is 8
initialize variables to store the values of args
converts the age to int and stores them in int variables
if this conversion fails exit ... this is done with the try - catch
block
compute average
print names
print average age
if number of args not equal to 8... print an error and end the program
please let me know if you have any questions
Cheers
Dasa
*******************************************************************************************************
if (args.length==8){
String name1 = null;
int age1 = 0;
String name2 = null;
int age2 = 0;
String name3 = null;
int age3 = 0;
String name4 = null;
int age4 = 0;
try {
name1 = args[0];
age1 = Integer.parseInt(args[1]);
name2 = args[2];
age2 = Integer.parseInt(args[3]);
name3 = args[4];
age3 = Integer.parseInt(args[5]);
name4 = args[6];
age4 = Integer.parseInt(args[7]);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Number format exception encountered. Did
you enter numbers? EXITING");
System.exit(0);
}
int averageAge;
averageAge = (age1 + age2+ age3 + age4) / 4;
System.out.println("names are: "+name1+" "+name2+" "+name3+"
"+name4);
System.out.println("averageAge is "+ averageAge);
}
else{
System.out.println("Invalid number of arguments supplied");
}
*******************************************************************************************************
On Thu, Jul 30, 2009 at 12: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
-~----------~----~----~----~------~----~------~--~---