| | |
a loop that extracts each digit of a number
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
i want to extract
576
to
hundreds = 5
tens = 7
ones = 6
my code does this purpose, but..
i want to rewrite my code using loop and modulus operator..
help guys. thanks
btw: i'm writing a program that converts numbr to w.ord-/.
576
to
hundreds = 5
tens = 7
ones = 6
my code does this purpose, but..
java Syntax (Toggle Plain Text)
h=num/100; t=(num-(h*100))/10; o=((num-(h*100))-(t*10));
i want to rewrite my code using loop and modulus operator..
help guys. thanks
btw: i'm writing a program that converts numbr to w.ord-/.
a joke.
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
Convert integer number to String. Length of of this String gives you info, how many digits are. Create int[string.length()] array. Use loop to transcribe all values. On fly convert particular values to int.
Find a better solution (using % and / operators).
Find a better solution (using % and / operators).
Last edited by quuba; Sep 7th, 2009 at 3:17 am. Reason: added last line
•
•
Join Date: Aug 2009
Posts: 7
Reputation:
Solved Threads: 0
Hi!
I suggest you should read on this and you will find the solution easily..
But as a help,
You can use % operator to simplify do it.
use % with / and you can simply do the job...
I hope this helps you..
I suggest you should read on this and you will find the solution easily..
But as a help,
You can use % operator to simplify do it.
Java Syntax (Toggle Plain Text)
int a=563%100; //a=63 int b=a%10; //b=3
I hope this helps you..
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
Laboratory:
array is a good place, to store calculated values.
Write once, use many times.
Continue...
java Syntax (Toggle Plain Text)
public class Convert { //1. base form static int[] convert(int k) { int[] result = null; return result; } static int[] convert2(int k) { int size = 3;//arbitrary int[] result = new int[size]; return result; } static int[] convert3(int k) { int size = 8; int[] result = new int[size]; for (int i = 0; i < size; i++) { result[i] = k % 10; // what can i do with k? } return result; } public static void main(String[] args) { int k = 576; int[] result = convert(k); //int[] result = convert2(k); //int[] result = convert3(k); System.out.println(k); System.out.println(result[0]); System.out.println(result[1]); System.out.println(result[2]); //System.out.println(result[3]); } }
Write once, use many times.
Continue...
![]() |
Other Threads in the Java Forum
- Previous Thread: Load data infile *.abc dynamically
- Next Thread: javax.comm.ComPortIdentifier
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt binary bluetooth businessintelligence busy_handler(null) card chat class classes client code collision component constructor crashcourse database draw eclipse ee error eventlistener exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jni jpanel jtree julia jvm linux list loop machine map method methods migrate mobile netbeans newbie oracle parsing physics plazmic print problem program programming project radio recursion scanner screen server service set sharepoint size smart sms socket software sort sortedmaps sql string swing textfield threads tree trolltech unlimited utility webservices windows






