Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (789) Games in Android Showcase (234) games submitted by our members Games in WIP (864) 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 8090 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

« JGO Overlord »

Medals: 1357
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

« JGO Overlord »

Medals: 1357
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 Wizard

Medals: 145
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

« JGO Overlord »

Medals: 1357
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

JGO Kernel

Medals: 518

 « 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 Wizard

Medals: 145
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

« JGO Overlord »

Medals: 1357
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

JGO Kernel

Medals: 518

 « 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 Ninja

Medals: 71

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.
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

JGO Kernel

Medals: 518

 « 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 Wizard

Medals: 145
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

« JGO Overlord »

Medals: 1357
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

JGO Kernel

Medals: 518

 « Reply #22 - Posted 2011-08-13 22:11:41 »

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

 hadezbladez (2631 views) 2018-11-16 13:46:03 hadezbladez (949 views) 2018-11-16 13:41:33 hadezbladez (2590 views) 2018-11-16 13:35:35 hadezbladez (510 views) 2018-11-16 13:32:03 EgonOlsen (3742 views) 2018-06-10 19:43:48 EgonOlsen (4140 views) 2018-06-10 19:43:44 EgonOlsen (2496 views) 2018-06-10 19:43:20 DesertCoockie (3304 views) 2018-05-13 18:23:11 nelsongames (3400 views) 2018-04-24 18:15:36 nelsongames (4377 views) 2018-04-24 18:14:32
 cygnus 15x Spasi 12x SkyAphid 12x orange451 12x FabulousFellini 9x VaTTeRGeR 8x KaiHH 8x philfrei 7x SHC 5x NuclearPixels 5x BitBotz 5x Speiger 4x elect 3x orangepascal 3x Ecumene 2x yboya 2x
 Java Gaming Resourcesby philfrei2019-05-14 16:15:13Deployment and Packagingby philfrei2019-05-08 15:15:36Deployment and Packagingby philfrei2019-05-08 15:13:34Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45
 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