• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking for negative numbers in method parameters  RSS feed

 
Venny Tank
Greenhorn
Posts: 7
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm also a java beginner, and on the same boat as you Darrin,
just stuck on the same exercise and after an hour trying write some code myself that didn't work, found this forum and read some other sources also.
So this is what I came up with, don't know if it is the right way, but it's working.



actually it is not the complete program from the exercise, it should check first if the integer (n) entered from the user is positive.
and also I think there is another approach to this exercise:



the problem is when enter a negative value, my positive value checker is not complete
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think this was a new question, albeit related to the topic of this thread, so I split it off.

Do you mean you have a precondition in that method that it must be passed a positive number as its argument? In that case, enforce the precondition: look at this constructor as an example. And this fictitious method:
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and note the subtle difference between non-negative and positive.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!