Results 1 to 10 of 10
  1. #1
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default print out error method for wrong args.

    Please explain how you would create an error message if the user inputs the incorrect arguments fir a set method.

    Java compiler does that for programmers, but how can we do that for user. print out an error message.

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Ehh to create your own Exception then you simple extend it:
    Java Code:
    class invailedInput extends Exception{
    }
    if thats what ur meening..

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

    Default

    Java Code:
    System.out.println("Look ma, an error message");
    You are going to have to be more specific if you want a more descriptive answer.

  4. #4
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    lets say there is a method setName and we only want it to take string args.

    the user then enters an int or a double. what would we code in the setName method that prints out an error message if the user enters anything but a string.

    Thanks

  5. #5
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Well... an int or double can be represented as a string. :P

    Do you want to filter out number characters [0-9] from a string?
    My Hobby Project: LegacyClone

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

    Default

    We could try to convert the string to a number using Integer.parseInt (try it and see what happens). Then if we get an exception we catch it and print our message to the user.

  7. #7
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    We could try to convert the string to a number using Integer.parseInt (try it and see what happens). Then if we get an exception we catch it and print our message to the user.
    Thats what I would like to know, how would I code the "get an exception".

    If the setName method wnats a string for a name, but someone enters 54(even as a string) , how can I code the method to only take certain args, otherwise print an error message?

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

    Default

    Refine your requirements first. What are valid names?

  9. #9
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    Create a class called Airplane which represents a commercial passenger plane. The class should
    contain data about the following:
    1. the airplane's altitude
    2. the airplane's bearing
    3. the airplane's current speed
    4. the airplane's model

    This exercise could be amended with error-checking in all the set methods, for instance no negative
    altitude, bearing should be n, nw, s, sw, etc. If you try to use a set method with a value that is wrong
    you should print an error-message.

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

    Default

    Nowhere d we see anything about names. Validating for numbers can be done by catching an exception upon an attempt to convert to number as I described above.
    No negative rule is easily enforced by a simple if statement.

Similar Threads

  1. What did i do wrong on thing method?
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 08:37 AM
  2. Replies: 0
    Last Post: 02-28-2009, 04:08 AM
  3. Error Eclipse &drjava displays wrong language
    By fenderman in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 03:24 AM
  4. Replies: 9
    Last Post: 11-22-2008, 11:03 PM
  5. Replies: 1
    Last Post: 11-11-2008, 08:18 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
  •