Parse Numbers??


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Parse Numbers??

  1. #1
    Join Date
    Aug 2006
    Posts
    4

    Parse Numbers??

    All right - here's what I needed to do - consider this a class in a grand scheme of classes - it uses proprietary code (Bajaui) in a Java environment -


    I needed to take an input variable int, up to 99,999 and take each of the individual numbers out as individual numbers.....


    For instance, the number 83746 would yield 8, 3, 7, 4, 6 -

    I thougth about sending it to an array - but not sure the syntax for parsing this...

    This is what I came up with: and it works -

    int m10k, m1k, m100, m10, m1;
    int KWUsage = (int)getIn10().getNumeric();
    System.out.println(KWUsage);

    m10k = (KWUsage/10000);
    System.out.println("m10k =" + m10k);
    m1k = ((KWUsage - (m10k*10000))/1000);
    System.out.println("m1k =" + m1k);
    m100 = ((KWUsage - (m10k*10000)) - (m1k*1000))/100;
    System.out.println("m100 =" + m100);
    m10 = ((KWUsage - (m10k*10000)) - (m1k*1000) - (m100*100))/10;
    System.out.println("m10 = " +m10);
    m1 = ((KWUsage - (m10k*10000)) - (m1k*1000) - (m100*100) - (m10*10));
    System.out.println("m1 = " +m1);


    getOut().setValue(m10k);
    System.out.println("The Value of Out 1 is " + getOut().getValue());
    getOut1().setValue(m1k);
    System.out.println("The Value of Out 2 is " + getOut1().getValue());
    getOut2().setValue(m100);
    System.out.println("The Value of Out 3 is " + getOut2().getValue());
    getOut3().setValue(m10);
    System.out.println("The Value of Out 4 is " + getOut3().getValue());
    getOut4().setValue(m1);
    System.out.println("The Value of Out 5 is " + getOut4().getValue());


    NOTE: the getOut#() methods and the getIn10() method are part of the proprietary code - if someone can suggest a way to array this I would appreciate it....

    I also had to cast the getIn10 to a int because by default, it's a double....

    Thanks

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    "number" / 10^n-1 = digit in "n-th" from the right digit
    output or save the digit
    then "modulus" by 10^n-1 to get the "remainder" which gets assigned to "number"
    loop down to 1

    trick is knowing how many digits in your number - your initial value of "n" - however, if you will never have a number larger than 99,999, then n = 5
    Last edited by nspils; 08-04-2006 at 08:19 AM.

  3. #3
    Join Date
    Jul 2005
    Posts
    78
    You could do the first bit like this:

    Code:
          //max int = 99,999
          int[] m = {0,0,0,0,0};
          int kw = 83746; //(int)getIn10().getNumeric();
    
          System.out.println(kw);
    
    
          // stores individual ints in the array.
          // the index of the array is the power of ten that the digit is.
          // eg: for 83746, 4 is stored in m[1] = 4 * 10^1
          for(int i = 0; i < m.length; i++)
          {
             m[i] = kw % 10;
             kw = kw / 10;
          }
    
          // this prints out the array values in reverse order so that 
          //    they appear correctly on the screen
          
          for(int i = m.length - 1; i >= 0; i--)
          {
             System.out.print(m[i] + " ");
          }

    Don't know how to do the next bit....

Similar Threads

  1. Replies: 8
    Last Post: 06-19-2006, 06:05 AM
  2. Replies: 1
    Last Post: 05-31-2006, 03:14 AM
  3. ID Numbers are out of sequence
    By sensei in forum VB Classic
    Replies: 1
    Last Post: 05-18-2006, 02:25 PM
  4. Replies: 0
    Last Post: 05-27-2005, 02:23 AM
  5. When to parse XML strings
    By J. Crew in forum XML
    Replies: 0
    Last Post: 04-23-2001, 05:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center