• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Checking for negative numbers in method parameters

 
Greenhorn
Posts: 7
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 72921
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72921
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and note the subtle difference between non-negative and positive.
 
reply
    Bookmark Topic Watch Topic
  • New Topic