>
> public class OwnCommandLineArguments {
>
>     public static void main(String[] args) {
>
>         System.out.println("Please input a name followed by an age.  " +
>                             "Do this at for at least three names; six names " 
> +
>                             "max, all on the same line.");
>
>         // Check if a command line argument exists
>         if(args.length == 0)
>             System.exit(0);
>
>         if ((args.length<6)||(args.length>12)){
>             System.out.println("Please input a name followed by an age.  Do " 
> +
>                     "this at for at least three names; six names max, all on 
> " +
>                     "the same line.");
>             System.exit(0);
>         }
You should initialize here the aveAge parameter, so that it will be
known outside of the subsequent for loop:
int aveAge = 0;
>
>         // Calculate the average age
>         for(int counter=2; counter<args.length; counter=counter+2){
>             int aveAge = Integer.parseInt(args[counter] + args[counter-2]);
then inside your loop you use just aveAge, you don't reinitialize with
int AveAge, otherwise you have another aveAge which is known only
inside the loop.
Actually here you don't need to rewind the loop to check
args[counter-2)], since you have already aveAge which contains either
0 before the loop, or the previous sum if you use:
aveAge = Integer.parseInt(args[counter]) + aveAge;
>         }
There is also a problem with the loop parameters. There are ways to
solve it, but you may help you if you print inside the loop the value
of the counter. See you begin with counter = 2, and read args(2), that
is you don't read the first integer. You may change it to:
                for(int counter=0; counter<args.length; counter=counter+2){
                            aveAge = Integer.parseInt(args[counter+1]) + aveAge;
                }
Here I begin with 0 for counter but I parse counter + 1; that is I get
the first number, since the first argument is a name, not a number.
>
Here again you should not reinitialize aveAge, but just use aveAge
= ...
>         int aveAge = (aveAge/(args.length/2);
>
>         System.out.println("The average age is " + aveAge);
Again you should initialize maxName as an empty String outside of the
loop;
String maxName = "";
and just use maxName inside the loop.
>         // Calculate the longest name
>         for(int counter=3; counter < args.length; counter= counter+2){
Again here there is a problem with the way you loop, again various
ways to do it. Again, if you print the value of counter and maxName
inside the loop it will help you to find the errors. Something like
this:
        String maxName = "";
        System.out.println("args" + args.length);
        for(int counter=args.length - 1; counter > 3; counter= counter
-2){
           System.out.println(counter);
           if (args[counter].length() > maxName.length()){
                maxName=args[counter -1];
                System.out.println("maxName= " + maxName);
            } else {
                maxName=args[counter-3];
                System.out.println("maxName: " + maxName);
            }
        }

Here I print the args length before the loop, just to be sure.
I loop from the end of the args array, ensuring that it does not go
under 3, and stepping backwards 2
then I read the name (that is why I begin with the n-1th element of
the array, which is a name, the n-th is a number)
I compare it with the maxName length, which is 0 at the beginning,
because we have initialized it to an empty string, and which is the
previous maxName length afterwards.
Then I store in maxName the value of the precedent name in the args
array, which explains the counter -1 parameter, in case the current
value is bigger than the older.
Or the value of the ante-precedent name in the args arrays, which
explains the counter -3 parameter, in case the current value is less
than the older.

Hope it helps.

>             if (args.length[counter]) > (args.length[counter-2]){
>                 string maxName=args[counter];
string class does not exist, String class exists.
>             else
>                 string maxName=args[counter-2];
the same here, use the right String class.
>         System.out.println("The longest name is:" + maxName);
>             }
>         }
>     }
>
>
>
> }

-- 
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

To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to this email 
with the words "REMOVE ME" as the subject.

Reply via email to