wood burning stoves 2.0*
The moose likes Cattle Drive and the fly likes Cattle Drive Problem 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Cattle Drive Problem 1" Watch "Cattle Drive Problem 1" New topic
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: 11444
    
  16

why do you have this line:

int size = args.length;


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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

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: 11444
    
  16

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: 18914
    
  40


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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Cattle Drive Problem 1