Results 1 to 12 of 12

Thread: int problem

  1. #1
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default int problem

    Hi

    Is there a way to make java recognize only the beginning of and integer.
    Lets say for example I have the number 200002 and I want the if condition to be

    if((a==field.getInt()) && (a==2.......))
    syso("bla bla");

    I want the program to recognize only the first number. I remember doing this with strings and it was something like "2%" to mark that the symbols after 2 were irrelevant to the search criteria, only the first symbol

    Much oblige

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It looks to me as if you're more interested in the String representation of the number rather than the numeric properties of the number. One possible solution is to translate the number into a String and extract the info you want. This can be done via the String.valueOf(...) method. If you know that the number always has x digits, though you could do this via int division perhaps. I'm curious though, you ever be checking for "0"?

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    As far as I know there isn't a way to look at the first digit of an integer without passing it to a string, string splitting or comparision, and then passing it back.

    What are you trying to accomplish anyways? I don't see why this would be useful.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    I have a field and a couple of values in a DB.
    certain user types begin with the int 2, certain with 1.
    Im trying to create a way for the frame to distinguish between them when one tries to validate. So for example when a user that doesnt have permission for that particular frame he gets a failure message.

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

    Default

    Quote Originally Posted by Dark View Post
    As far as I know there isn't a way to look at the first digit of an integer without passing it to a string, string splitting or comparision, and then passing it back.
    Java Code:
    public int getFirstDigit(int n) { // assumes n >= 0
       while (n >= 10) n/= 10;
       return n;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Quote Originally Posted by jeata View Post
    I have a field and a couple of values in a DB.
    certain user types begin with the int 2, certain with 1.
    Im trying to create a way for the frame to distinguish between them when one tries to validate. So for example when a user that doesnt have permission for that particular frame he gets a failure message.
    Is the db field a NUMBER then?

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    EDIT: Ha, I saw the error in my post.

    If you added a check for 0 and negatives then the method JosAH posted would be perfect for what you're looking for. Personally I would've used a DB with flags for user priveledges, but I will leave you to your design.
    Last edited by Dark; 05-12-2011 at 06:43 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Is the db field a NUMBER then?
    Yes the value in the DB is a number

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Seems wrong to me to assign meaning like that to individual digits in a NUMBER field, but I suppose you have to work with what you're given.

    Still, it means you can't search on all 1's or 2's easily (by easily I mean easily for the db engine)...sounds like a flaw in the db design to me.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Oh, and for Dark:
    Java Code:
    while (n >= 10) n/= 10;
    this will loop until n is a single digit, whatever size n was originally.

    ETA: Doesn't stop Jos from being a smartarse, though...;)

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @Tolls Yeah I didn't full read to comprehend JosAH's code before I hit post, then I reread it and smacked my head on the desk. Apparently didn't complete the edit before someone noticed.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

Posting Permissions

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