| | |
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: 54
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: 54
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
Views: 336 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation awt binary bluetooth businessintelligence busy_handler(null) card chat class classes client code collision component constructor database draw eclipse error event eventlistener exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me jar java javafx javamicroeditionuseofmotionsensor javaprojects jmf jni jpanel jtree julia link linux list loop machine map method methods mobile netbeans newbie number object oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms socket sort sortedmaps sql string swing test textfield threads time transfer tree unlimited utility webservices windows






