Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (606) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 First Number from Integer  (Read 2583 times) 0 Members and 1 Guest are viewing this topic.
GustavXIII

Junior Devvie

Projects: 1

 « Posted 2011-08-10 17:35:52 »

Hi!
I want to draw just the first Number from Integer.
If I have the number 12009, I want to draw only the 1.

In C there was something like number%d or something like that.
How does it work for java? It is because I want to make a Timer for the game.
namrog84

JGO Ninja

Medals: 46
Projects: 4

Keep programming!

 « Reply #1 - Posted 2011-08-10 17:40:08 »

in java the % is called a modulus

so lets say you have 13%10  that would equal 3
or 11%10 would equal 1,   it gives the remainder after the division.

this will occur every time timer is divisible by 10, so 1,2,3,4,5,6,7,8,9 would NOT trigger it
 1  2  3  4  5 `//loopif(timer % 10 == 0){    //ticks every 10th interval}timer++;`

This is 1 of many way some people do some sort of interval timer.

You could also just set up a simple little loop to break each number into something separate

How exactly do you want to implement your timer? is it forever counting up like elapsed time? or firing/action interval? or whatever, as there may be a better solution for your given scenario.

\/ what he said works too \/

"Experience is what you get when you did not get what you wanted"
Riven
« League of Dukes »

« JGO Overlord »

Medals: 949
Projects: 4
Exp: 16 years

 « Reply #2 - Posted 2011-08-10 17:42:47 »

he wants the left-most digit:

a quick and dirty solution would be:
 1 `int digit = String.valueOf(Math.abs(i)).charAt(0)-'0';`

how this relates to timers is a mystery to me.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
GustavXIII

Junior Devvie

Projects: 1

 « Reply #3 - Posted 2011-08-10 18:38:21 »

Ok thanks!
@namrog In Slick I using delta. If delta is 1000 its 1 second. But I cant show 1000 in the menu so I have to reduce it to the first digit(?).
 « Reply #4 - Posted 2011-08-10 18:44:36 »

Divide by 1000
Riven
« League of Dukes »

« JGO Overlord »

Medals: 949
Projects: 4
Exp: 16 years

 « Reply #5 - Posted 2011-08-10 18:53:08 »

If I have the number 12009, I want to draw only the 1.
dividing by 1000 won't result in the example the OP posted.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
delt0r

JGO Knight

Medals: 33
Exp: 18 years

Computers can do that?

 « Reply #6 - Posted 2011-08-12 10:23:27 »

This would also work.

 1  2  3  4  5  6 `import static Math.*;public int firstDig(double x){   double divisor=pow(10,floor(log10(x)));   return (int)(x/divisor);}`

This can be extended easily for any number base.

I have no special talents. I am only passionately curious.--Albert Einstein
pitbuller
 « Reply #7 - Posted 2011-08-12 10:28:29 »

If I have the number 12009, I want to draw only the 1.
dividing by 1000 won't result in the example the OP posted.
I think its pretty clear that op ask wrong question. I think he want seconds from milliseconds and somehow messed question.

Senior Devvie

Medals: 3
Projects: 1

Java games rock!

 « Reply #8 - Posted 2011-08-12 10:39:06 »

int a = 12009;
int b = (int)(a / Math.pow(10, (int)Math.log10(a))); // b --> 1

Math.abs() if a is negative.

Edit: Ok, just saw delt0r posted the same.
JL235

JGO Coder

Medals: 10

 « Reply #9 - Posted 2011-08-12 12:31:33 »

Personally, I would go with Rivens String version, but here is an over-engineered alternative:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `public void firstInt( int n ) {    boolean positive = false;    if ( n < 0 ) {        n = Math.abs(n);        positive = true;    }    for ( int base = 10; base < 1000000000; base *= 10 ) {        if ( n < base ) {            int result = (int) ( n / (base/10.0) ) ;            return positive ? result : -result ;        }    }    throw IllegalStateException( "should never be able to reach this point" );}`

It could also be expanded into a long list of if's.

Riven
« League of Dukes »

« JGO Overlord »

Medals: 949
Projects: 4
Exp: 16 years

 « Reply #10 - Posted 2011-08-12 13:22:21 »

faster faster!

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25 `   public static int firstDigit(int n)   {      int mul;      if (n < 0)      {         mul = -1;         n = -n;      }      else      {         mul = +1;      }      for (int base = 10; base <= 1000000000; base *= 10)      {         if (n < base)         {            return mul * n * 10 / base;         }      }      throw new IllegalStateException("should never be able to reach this point");   }}`

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Roquen
 « Reply #11 - Posted 2011-08-13 08:00:23 »

If you really wanted to go overboard you could convert the linear search into a binary one.
counterp

Senior Devvie

Medals: 11

 « Reply #12 - Posted 2011-08-13 08:06:44 »

only the original one Riven posted works properly   although addictman suggested using Math.abs for negatives so I guess he's good too
delt0r

JGO Knight

Medals: 33
Exp: 18 years

Computers can do that?

 « Reply #13 - Posted 2011-08-13 09:56:46 »

It won't if the argument is negative. You get "-" or whatever negative convention the local uses. I tested my stuff in R --seem to work fine.

I have no special talents. I am only passionately curious.--Albert Einstein
Riven
« League of Dukes »

« JGO Overlord »

Medals: 949
Projects: 4
Exp: 16 years

 « Reply #14 - Posted 2011-08-13 12:13:36 »

If you really wanted to go overboard you could convert the linear search into a binary one.
While this is true, I think it's likely that in this case, most values will be less than 100.000, in which case it's pretty hard to beat the linear search.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
JL235

JGO Coder

Medals: 10

 « Reply #15 - Posted 2011-08-13 14:03:26 »

If you really wanted to go overboard you could convert the linear search into a binary one.
While this is true, I think it's likely that in this case, most values will be less than 100.000, in which case it's pretty hard to beat the linear search.
You could increment the base by 100 instead of 10, something like...
 1  2  3  4  5  6  7  8  9 `for (int base = 10; base <= 1000000000; base *= 100) {    if (n < base) {        if ( n < base/10 ) {            return mul * n * 10 / (base/10);        } else {            return mul * n * 10 / base;        }    }}`

This would almost half the number checks.

Roquen
 « Reply #16 - Posted 2011-08-13 19:24:52 »

I thought we were over-engineering the problem.  Here's an untested entry:

 1  2  3  4  5  6 `  public static int firstDecimalDigit(int n)  {    n = Math.abs(n);        return (int)Math.floor(n*Math.pow(10, -((int)Math.log10(n))));  }`

edit: I somehow missed the two previous in the same vein...dang!
Abuse

JGO Knight

Medals: 17

falling into the abyss of reality

 « Reply #17 - Posted 2011-08-13 19:55:16 »

I'm sure you're all aware that negation does not work for Integer.MIN_VALUE (and consequently neither does Math.abs(int)).

So all of the above implementations will malfunction in all kinds of interesting ways when the input n=Integer.MIN_VALUE.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
counterp

Senior Devvie

Medals: 11

 « Reply #18 - Posted 2011-08-13 20:01:56 »

If you really wanted to go overboard you could convert the linear search into a binary one.
While this is true, I think it's likely that in this case, most values will be less than 100.000, in which case it's pretty hard to beat the linear search.

Well in that case

 1  2  3  4  5  6  7  8  9 `   private static final int digits[] = new int[200001];   static {      for (int i = 0; i < digits.length; i++)         digits[i] = String.valueOf(Math.abs(i - 100000)).charAt(0)-'0';   }   public static int lookup(int n) {      return digits[n + 100000];   }`

lololololololol

-100000 to 100000

if you were worried about memory you could convert it to byte or something, or even do some bitshifting stuff to get maximum value out of the integers
Roquen
 « Reply #19 - Posted 2011-08-13 20:32:19 »

Well if you're doing anything like the stuff posted in the thread, then it's almost certain that: You're doing it wrongtm.
delt0r

JGO Knight

Medals: 33
Exp: 18 years

Computers can do that?

 « Reply #20 - Posted 2011-08-13 22:00:00 »

So Roquen, what is the right way to be doing it?

I have no special talents. I am only passionately curious.--Albert Einstein
Riven
« League of Dukes »

« JGO Overlord »

Medals: 949
Projects: 4
Exp: 16 years

 « Reply #21 - Posted 2011-08-13 22:04:39 »

I'm fairly certain he means that the 'problem' defined by the OP has some flaws, so the solution will be inherently wrong.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Roquen
 « Reply #22 - Posted 2011-08-13 22:11:41 »

Yes, that's exactly what I meant.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 theagentd (12 views) 2015-03-27 23:08:20 wxwsk8er (53 views) 2015-03-20 15:39:46 Fairy Tailz (46 views) 2015-03-15 21:52:20 Olo (29 views) 2015-03-13 17:51:59 Olo (30 views) 2015-03-13 17:50:51 Olo (39 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (57 views) 2015-03-10 11:36:42 ClaasJG (41 views) 2015-03-10 11:33:01 Pippogeek (49 views) 2015-03-05 14:36:23
 BurntPizza 21x LiquidNitrogen 21x basil_ 20x EgonOlsen 17x KevinWorkman 17x theagentd 16x Roquen 16x wessles 11x 65K 11x Varkas 10x Riven 10x princec 9x phu004 8x SHC 8x Ashedragon 8x Rayvolution 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org