-
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
-
"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 07:19 AM.
-
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
-
Replies: 8
Last Post: 06-19-2006, 05:05 AM
-
By kanakatam in forum Java
Replies: 1
Last Post: 05-31-2006, 02:14 AM
-
By sensei in forum VB Classic
Replies: 1
Last Post: 05-18-2006, 01:25 PM
-
Replies: 0
Last Post: 05-27-2005, 01:23 AM
-
Replies: 0
Last Post: 04-23-2001, 04: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks