How to extract digit from a command line

I have a question which i needed help.

For example i created this command line :

int five_digit = Integer.parseInt(JOptionPane.showInputDialog("Enter any 5 integer number"));

I wish to extract each number and mutiply it by a digit.

For example if I input 12345

I like to extract 1 and multiply it by 5. Extract 2 and multiply it by 4.....

How do I do it? Please advise

[412 byte] By [SummerCoola] at [2007-11-27 11:09:37]
# 1

1. Play around with x / 10 and x % 10

2. Keep user input as a String. Use charAt and then parseInt.

floundera at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 2

Sorry I dont get you...i am a beginer...hence would you elaborate further?

SummerCoola at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 3

12345 / 10000 = 1

12345 % 10000 = 2345

cotton.ma at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 4

If Math ain't your thing, stick with option 2. Or don't you understand that either?

floundera at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 5

yeah maths is not something i am good at and I am sure not too clever to understand ...sorry

SummerCoola at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 6

Instead of using parseInt at the beginning, save the full number as a String. Then, to get a given digit, use

int third_digit = Integer.parseInt (five_digit.substring (3, 4));

If you want to split every digit into an array:

public int[] getDigits (String n)

{

int[] digits = new int[n.length()];

for (int x = 0; x < n.length(); x++)

digits[x] = Integer.parseInt (n.substring (x, x+1));

return digits;

}

If you don't save it as a String, there is a lot of math involved to get a particular digit, but it is possible.

spanglercoa at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 7

> yeah maths is not something i am good at and I am

> sure not too clever to understand ...sorry

Oh I see, so you want us to give you the codes then?

cotton.ma at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 8

Follwoing is the Code :

import java.util.*;

public class Main

{

public static void main(String[] args)

{

int number=12345;

int remainder=0;

int[] digits=new int[5];

for(int i=0;(remainder=number%10)!=0;i++)

{

digits=remainder;

number=number/10;

}

for(int i=digits.length-1;i>0;i--)

System.out.println(digits);

}

}

Programmatically we have separated out every digit in remainder variable, & array of int is populated where you get all digits in extracted form.

if you have any doubt in the code or logic, please feel free to ask !!!

thanks,

Mukesh.

Mukesh.Bhojwania at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 9

public int[] getDigits (String n)

{

int[] digits = new int[n.length()];

for (int x = 0; x < n.length(); x++)

digits[x] = Integer.parseInt (n.substring (x, x+1));

return digits;

}

If you are going to spoonfeed them, why not just call toCharArray?

floundera at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 10

*sigh*

Excellent work boys. Keep up the fine effort.

cotton.ma at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 11

>

> If you don't save it as a String, there is a lot of

> math involved to get a particular digit, but it is

> possible.

I'm not sure I'd call division "a lot of math".

cotton.ma at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 12

> Follwoing is the Code :

I see everyone wants to get into the act. I'd use a while loop instead. As well as code tags.

floundera at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 13

Thanks...but when I try to run the code which you had advised. I've got a error message for the following in bold...telling me it is of imcompatible type....please advise....

I like to reply to those who had provided help...I am not trying to ask for a full code...but I wish to learnt too...i have been sitting on my chair...for almost a day....thinking how do I separate such a task....

But I am very glad for your kind and generous help...thank you...=)

> Follwoing is the Code :

>

> import java.util.*;

>

> public class Main

> {

> public static void main(String[] args)

> {

> int number=12345;

> int remainder=0;

> int[] digits=new int[5];

> for(int i=0;(remainder=number%10)!=0;i++)

> {

> digits=remainder;

> number=number/10;> }

>

> for(int i=digits.length-1;i>0;i--)

> System.out.println(digits);

> }

> }

>

> Programmatically we have separated out every digit in

> remainder variable, & array of int is populated where

> you get all digits in extracted form.

>

> if you have any doubt in the code or logic, please

> feel free to ask !!!

>

> thanks,

> Mukesh.

SummerCoola at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 14

> Thanks...but when I try to run the code which you had

> advised. I've got a error message for the following

> in bold...telling me it is of imcompatible

> type....please advise....

Then don't cheat and copy someone else's code. Figure it out for yourself and write your own code. I gave you two perfectly good ways to do it in reply #1. All you have to do is think about how they work. If you have any trouble post YOUR code (in between code tags) and ask a specific question.

floundera at 2007-7-29 13:36:33 > top of Java-index,Java Essentials,Java Programming...
# 15

Little elementary way! :-)

But watch for any syntax errors :)

int five_digit=12345;

int ar[]={0,0,0,0,0};

int j=0;

int i=0;

while( five_digit !=0 ) {

i=five_digit%10;

ar[j]=i;

System.out.println(i);

j++;

five_digit=five_digit/10;

}

passion_for_javaa at 2007-7-29 13:36:38 > top of Java-index,Java Essentials,Java Programming...
# 16

Thanks for you advise and teaching...I will do my best to work something out...will once again come back here to trouble you guys if I do encounter further issue...

SummerCoola at 2007-7-29 13:36:38 > top of Java-index,Java Essentials,Java Programming...
# 17

> >

> > If you don't save it as a String, there is a lot

> of

> > math involved to get a particular digit, but it is

> > possible.

>

> I'm not sure I'd call division "a lot of math".

Oops. Forgot about modulus. I was referring to the mathematical way to do this...without modulus. Oh well.

spanglercoa at 2007-7-29 13:36:38 > top of Java-index,Java Essentials,Java Programming...
# 18

> ...

> Oops. Forgot about modulus. I was referring to the

> mathematical way to do this...without modulus. Oh

> well.

So you're saying that the term modulus has no mathematical meaning?

prometheuzza at 2007-7-29 13:36:38 > top of Java-index,Java Essentials,Java Programming...
# 19

> > ...

> > Oops. Forgot about modulus. I was referring to the

> > mathematical way to do this...without modulus. Oh

> > well.

>

> So you're saying that the term modulus has no

> mathematical meaning?

No, when did I say that?

spanglercoa at 2007-7-29 13:36:38 > top of Java-index,Java Essentials,Java Programming...
# 20

> > ...

> > So you're saying that the term modulus has no

> > mathematical meaning?

>

> No, when did I say that?

You didn't say it directly. I thought you implied it by saying "I was referring to the mathematical way to do this...without modulus". Guess I was wrong in my assumption.

prometheuzza at 2007-7-29 13:36:38 > top of Java-index,Java Essentials,Java Programming...