Results 1 to 5 of 5

Thread: constructors?

  1. #1
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default constructors?

    hello everyone. when working with objects, how would one specify that the input of negative numbers is not allowed in the constructor part of the code?

  2. #2
    gcampton Guest

    Default

    if (int <= 0)
    int = 1;

    Is this what you mean?

  3. #3
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    let me specify. i am supposed to make a program with objects where the user enters data and it reads it back from the seperate object class...now i am pretty sure the constuctor part of the code is what i need to modify..

    but the user is not allowed to enter a negative number. can't seem to figure out what i would need to do to read back an error message or something if the user enters a negative number

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    You can throw an IllegalArgumentException that you catch and let the user know of their foolishness when it is thrown. If you have a setter for that field then you need to make sure that you don't leave any holes.

  5. #5
    gcampton Guest

    Default

    yea or you can be nicer and keep the program running in the setter for the params do a check and if it's negative then print error message, return n for get, ask for positive and remake the class object with the new input. Loops ftw.

Similar Threads

  1. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 06:46 AM
  2. [SOLVED] Noobie Help! Constructors
    By fullmetaljacket in forum New To Java
    Replies: 6
    Last Post: 05-14-2009, 02:52 AM
  3. Need some help with a task, constructors.
    By JKM in forum New To Java
    Replies: 1
    Last Post: 02-01-2009, 11:09 PM
  4. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 07:59 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •