Results 1 to 12 of 12
Thread: int problem
- 05-12-2011, 06:53 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
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
-
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"?
- 05-12-2011, 06:58 PM #3
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.
- 05-12-2011, 07:12 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
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.
- 05-12-2011, 07:16 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-12-2011, 07:34 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
- 05-12-2011, 07:35 PM #7
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 07: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.
- 05-12-2011, 07:37 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
- 05-12-2011, 07:40 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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.
- 05-12-2011, 07:41 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Oh, and for Dark:
Java Code:while (n >= 10) n/= 10;
ETA: Doesn't stop Jos from being a smartarse, though...;)
- 05-12-2011, 07:44 PM #11
@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.
- 05-13-2011, 10:19 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Bookmarks