On Mar 27, 4:58 am, Tosh <[email protected]> wrote: > I've been going over this for a while. It worked once with 4 entries. Then > it stopped working but I did not make any changes that I was aware of. The > initial test for arguments works. if it is not 3 - 6 pairs entered it will > exit and complain. > > For some reason if you enter 3 - 6 pairs it runs successfully but never > outputs the end result. I put code in the loop to see if it would output > that and it does not seem to be getting that far either. > > Nothing highlights in red in netbeans so there must be some logic error that > I don't understand. > > /** > * @author Tosh > */ > public class CommandLineArguments { > > public static void main(String[] args) { > // Check if a command line argument exists > if((args.length <6)||(args.length>12)) You may want to test also that the number of arguments entered is even (see % to do this - args.length % 2 != 0) and you miss the { at the end of the if line since you have more than one conditional line (by the way it is better to always put a { sign even with one line) You may also drop the internal parentheses in the if, you need them when you have more than two conditions to separate the third condition from the first two ones, as the if understand only two conditions basically: cond1 || cond2 cond1 || (cond2 || cond3) etc... same with && > System.out.println("Please enter 3 to 6 name age pairs."); > System.exit(0); You miss here a } sign to close the if > //create variables > int agetotal = 0; > int ageaverage = 0; > int entries = args.length / 2; > > //loop it to add them up > for(int i = 0; i<args.length; i++){ > i++; //increment again to get the odd array numbers Here you may loop using an i=i+2 parameter instead of incrementing the parameter inside the loop for (int i = 0; i < args.length; i = i+2) > agetotal = Integer.parseInt(args[i]) + agetotal; here you peek the i + 1 arguments: agetotal = Integer.parseInt(args[i+1]) + agetotal; > System.out.println("current age total is: " + agetotal); > } > > //divide to get the average and display it > ageaverage = agetotal/entries; > System.out.println("The average age is " + ageaverage); > } > > > > }
Hope it helps. -- 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.
