Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  First Number from Integer  (Read 2374 times)
0 Members and 1 Guest are viewing this topic.
Offline GustavXIII

Junior Duke


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.
Offline 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  
//loop
if(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"
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GustavXIII

Junior Duke


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(?).
Offline Gudradain
« Reply #4 - Posted 2011-08-10 18:44:36 »

Divide by 1000
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline delt0r

JGO Knight


Medals: 27
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
Offline 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.
Offline Addictman

Senior Duke


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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« 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");
   }
}


Tongue

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline 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.
Offline counterp

Senior Duke


Medals: 11



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

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

JGO Knight


Medals: 27
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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


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

Offline 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!
Offline Abuse

JGO Knight


Medals: 13


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
Offline counterp

Senior Duke


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

JGO Knight


Medals: 27
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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (26 views)
2014-10-26 10:14:04

theagentd (32 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!