| Author |
Cattle Drive Problem 1
|
Aman Singh
Ranch Hand
Joined: Oct 30, 2008
Posts: 57
|
|
Hi folks,
I just got done with the first problem of Cattle Drive (Printing a name 100 times).
I am getting the correct output, but do you guys see any issues with this code or something I can do to improve it. I know its a real small problem
[edit: we do not allow students to post actual code for the Cattle Drive, since it would deprive other students of learning opportunities]
|
 |
Sridhar Santhanakrishnan
Ranch Hand
Joined: Mar 20, 2007
Posts: 317
|
|
|
It would print only 99 times.. not 100.
|
 |
Aman Singh
Ranch Hand
Joined: Oct 30, 2008
Posts: 57
|
|
Hi Sri,
Thanks for the reply
I apologize I kept it that way. Any other issue?
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9943
|
|
why do you have this line:
int size = args.length;
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Sridhar Santhanakrishnan
Ranch Hand
Joined: Mar 20, 2007
Posts: 317
|
|
|
And you would have a java.lang.ArrayIndexOutOfBoundsException if no parameters are entered. So use the size variable and check.
|
 |
Aman Singh
Ranch Hand
Joined: Oct 30, 2008
Posts: 57
|
|
Hi fred,
I actually wanted to handle ArrayIndexOutOfBounds, but couldnt implement it.
How do I handle this condition if user doesnt enter any arguments?
|
 |
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
|
posted

0
|
|
Check the length of the arguments array?
|
 |
Aman Singh
Ranch Hand
Joined: Oct 30, 2008
Posts: 57
|
|
Hi David,
Thanks
if length is zero, dont process.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9943
|
|
Aman Singh wrote:if length is zero, dont process. 
or set a default value...or tell the user they did something wrong...or anything else you want!
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16684
|
|
There are some very specific rules when working with the cattle drive -- of which I am not sure if you are violating them.
Regardless, this topic is being moved to the cattle drive forum, to be sorted out there.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: Cattle Drive Problem 1
|
|
|