| | |
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 |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit draw eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying oracle page pearl print problem program programming project qt recursion scanner screen server set size sms sort spamblocker sql string swing system thread threads time tree variablebinding windows xor






