Click to See Complete Forum and Search --> : Parse Numbers??


catman2112
08-03-2006, 05:10 PM
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

nspils
08-03-2006, 07:19 PM
"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

masher
08-03-2006, 07:20 PM
You could do the first bit like this:


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