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
1. Play around with x / 10 and x % 10
2. Keep user input as a String. Use charAt and then parseInt.
Sorry I dont get you...i am a beginer...hence would you elaborate further?
12345 / 10000 = 1
12345 % 10000 = 2345
If Math ain't your thing, stick with option 2. Or don't you understand that either?
yeah maths is not something i am good at and I am sure not too clever to understand ...sorry
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.
> 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?
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.
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?
*sigh*
Excellent work boys. Keep up the fine effort.
>
> 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".
> 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.
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.
> 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.
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;
}
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...
> >
> > 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.
> ...
> 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?
> > ...
> > 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?
> > ...
> > 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.