• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cattle Drive Problem 1

 
Aman Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would print only 99 times.. not 100.
 
Aman Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sri,

Thanks for the reply
I apologize I kept it that way. Any other issue?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do you have this line:

int size = args.length;
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you would have a java.lang.ArrayIndexOutOfBoundsException if no parameters are entered. So use the size variable and check.
 
Aman Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the length of the arguments array?
 
Aman Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

Thanks
if length is zero, dont process.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic