Calling individual digits of a number (Java)

B.def

Member
Jun 13, 2010
68
0
0
I need to pull out single digits out of a number

ex. For number 24362
The output should be:
First Digit: 2
Second Digit: 4
Third Digit: 3
Fourth Digit: 6
Fifth Digit: 2

I do not understand how to do this. My instructor told me to use "/ and %" but the only one I know that is right is my fifth digit below.

Code:
package javahomework;



    public class JavaHomework1pt2
    {
    public static void main( String [] args )
    {
        int number =24362;
        System.out.println("First digit = " + number %  1);
        System.out.println("Second digit = " + number % 1);
        System.out.println("Third digit = " + number %  1);
        System.out.println("Fourth digit = " + number % 1);
        System.out.println("Fifth digit = " + number % 10);



Thanks for any help. Either an explanation or a point to the right direction would be great. Thanks!
 

BigDH01

Golden Member
Jul 8, 2005
1,631
88
91
I need to pull out single digits out of a number

ex. For number 24362
The output should be:


I do not understand how to do this. My instructor told me to use "/ and %" but the only one I know that is right is my fifth digit below.

Code:
package javahomework;



    public class JavaHomework1pt2
    {
    public static void main( String [] args )
    {
        int number =24362;
        System.out.println("First digit = " + number %  1);
        System.out.println("Second digit = " + number % 1);
        System.out.println("Third digit = " + number %  1);
        System.out.println("Fourth digit = " + number % 1);
        System.out.println("Fifth digit = " + number % 10);



Thanks for any help. Either an explanation or a point to the right direction would be great. Thanks!

Would there only ever be 5 digits? The % operator is for grabbing remainder and / will basically round down or throw away any remainder. So, in Java, 53/10 = 5. This is why your fifth digit is correct. A number % 10 is going to return the last digit as this will be the remainder after the number is divided by 10. 3/10 = 0 and 3 % 10 = 3, or 0 with a remainder of 3.

What you basically want to do is traverse down and get rid of the previous digits. So 53432 / 10000 = 5 but now we have to get rid of 50000 before the next calculation. I'll leave it up to you to decide how to do this.
 

B.def

Member
Jun 13, 2010
68
0
0
Would there only ever be 5 digits? The % operator is for grabbing remainder and / will basically round down or throw away any remainder. So, in Java, 53/10 = 5. This is why your fifth digit is correct. A number % 10 is going to return the last digit as this will be the remainder after the number is divided by 10. 3/10 = 0 and 3 % 10 = 3, or 0 with a remainder of 3.

What you basically want to do is traverse down and get rid of the previous digits. So 53432 / 10000 = 5 but now we have to get rid of 50000 before the next calculation. I'll leave it up to you to decide how to do this.





Thanks you good sir. I think I got it.

Code:
    public class JavaHomework1pt2
    {
    public static void main( String [] args )
    {
        int number =24362;
        System.out.println("First digit = " + ((number)/(10000)) %  10);
        System.out.println("Second digit = " + ((number)/(1000)) % 10);
        System.out.println("Third digit = " + ((number)/(100)) %  10);
        System.out.println("Fourth digit = " + ((number)/(10))% 10);
        System.out.println("Fifth digit = " + number % 10);

Output:
Code:
run:
First digit = 2
Second digit = 4
Third digit = 3
Fourth digit = 6
Fifth digit = 2

Looks good to me! Thanks!
 

veri745

Golden Member
Oct 11, 2007
1,163
4
81
Looks good, but your solution is awfully hard-coded and not very flexible.

It would be a much more interesting problem to do the same for a number of arbitrary magnitude.

Code:
public int printDigits (int input)
{
  int whichDigit = 1;
  if (input > 10) {
    whichDigit = printDigits(input / 10);
  }

  System.out.println("Digit #" + whichDigit + " = " + input % 10);

  return whichDigit + 1;
}
 
Last edited:

B.def

Member
Jun 13, 2010
68
0
0
Yeah, but what do expect from a person who it's their first time programming and have only had a month of lessons from a professor who barely speaks English. lol
Edit: Run on sentence much? I think so.
 

veri745

Golden Member
Oct 11, 2007
1,163
4
81
Yeah, but what do expect from a person who it's their first time programming and have only had a month of lessons from a professor who barely speaks English. lol
Edit: Run on sentence much? I think so.

If you are conscious about how flexible and reusable your code is, I think you will find that your programming classes will go much more smoothly.

Class assignments tend to build on each other, and if you can reuse code from a previous assignment, it can save a lot of time.

This is just just as true in the real world as it is in class.
 

beginner99

Diamond Member
Jun 2, 2009
5,315
1,760
136
If you are conscious about how flexible and reusable your code is, I think you will find that your programming classes will go much more smoothly.

Class assignments tend to build on each other, and if you can reuse code from a previous assignment, it can save a lot of time.

This is just just as true in the real world as it is in class.

I actually once read an article by a Programmer/Developer claiming he very much likes to hire lazy developers because they tend to make better code exactly because of those reasons. (And work more efficient in general). Of course the lazy guy can't just leave 2 hours before everyone else and hence in the end he actually does a lot more work.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
I actually once read an article by a Programmer/Developer claiming he very much likes to hire lazy developers because they tend to make better code exactly because of those reasons. (And work more efficient in general). Of course the lazy guy can't just leave 2 hours before everyone else and hence in the end he actually does a lot more work.

Bleh, the term lazy is subjective then. I would NEVER hire a lazy programmer but if he works hard AND produces efficient and reusable code, then he is good to go.

WTF, that's like saying I like to hire fat people to cook in my restaurant because they taste food better.