Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default how can check input is double or integer?

    how can check input is double or integer?

  2. #2
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: how can check input is double or integer?

    There may be a better way of doing this but you could try something like this. Convert the input to a string if it isn't already. Then you could search through each character. If there is a decimal point then it is a double value else it is an integer.

    Hope this helps,

    Regards Serb.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: how can check input is double or integer?

    Quote Originally Posted by SerbianSergeant View Post
    There may be a better way of doing this but you could try something like this. Convert the input to a string if it isn't already. Then you could search through each character. If there is a decimal point then it is a double value else it is an integer.

    Hope this helps,

    Regards Serb.
    The String "123" can also be a double although there is no dot in it and "123E4" is only a double although there is also not dot in it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: how can check input is double or integer?

    Quote Originally Posted by JosAH View Post
    The String "123" can also be a double although there is no dot in it and "123E4" is only a double although there is also not dot in it.

    kind regards,

    Jos
    All integers can be doubles can't they? And good point, surely you could instead compare whether there are letters or a dot within the string? This may be incredibly inefficient, but just a suggestion.

    Regards Serb.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: how can check input is double or integer?

    Quote Originally Posted by SerbianSergeant View Post
    All integers can be doubles can't they? And good point, surely you could instead compare whether there are letters or a dot within the string? This may be incredibly inefficient, but just a suggestion.

    Regards Serb.
    That doesn't always work either, e.g. 12345678901 can only be a double because it doesn't fit in an int. The best bet is to parse it as an int and when an Exception is thrown, parse it as a double; if still an Exception is thrown it isn't any of those types.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: how can check input is double or integer?

    Good point, nice solution. HearT.Hunt3r, listen to Jos, forget about what i suggested ^^

  7. #7
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default Re: how can check input is double or integer?

    i got my solution last night and did that checking by Double.parseDouble() and Integer.parseInteger() (try/exception) ... thx 2 all

Similar Threads

  1. convert integer to double
    By himanwish in forum New To Java
    Replies: 4
    Last Post: 03-17-2011, 02:55 AM
  2. Adding an integer to a double?
    By tokoolio in forum New To Java
    Replies: 5
    Last Post: 01-17-2011, 07:57 PM
  3. How to check if enter is integer or not
    By hacikho in forum New To Java
    Replies: 6
    Last Post: 09-29-2010, 03:03 AM
  4. How to extract integer value out of a double value?
    By javaexplorer in forum AWT / Swing
    Replies: 4
    Last Post: 01-16-2010, 06:32 AM
  5. check if String is an integer?
    By McChill in forum New To Java
    Replies: 5
    Last Post: 05-02-2009, 08:51 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
  •