1

I have the code here but I'm not sure how to display the number of digits once I get the user input as an int.

public class Chapter8 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n;
        System.out.print("Enter a positive integer: ");
        n = in.nextInt();
        if (n <= 0) {
            while (n != 0) {
                System.out.println(String.format("%02d", 5));
        } 
    }
} 
intcreator
  • 4,206
  • 4
  • 21
  • 39
Vivid
  • 37
  • 5
  • 2
    Possible duplicate of [How do I format a number in java?](http://stackoverflow.com/questions/50532/how-do-i-format-a-number-in-java) – Ormoz Oct 02 '15 at 00:26
  • Are there any rules around how you have to read the data from the scanner? – Michael Oct 02 '15 at 00:42

4 Answers4

0

You can count by dividing the number by 10. each time the number will be shortened by 1 digit until the number reach 0 :

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n;
    System.out.print("Enter a positive integer: ");
    n = in.nextInt();
    int counter = 0;
    while(n > 0){
        n = n / 10;
        counter++;
    }
    System.out.println(counter);
}
chenchuk
  • 5,324
  • 4
  • 34
  • 41
0

One quick solution is to convert the integer into a string and then measure how many characters it has:

String myNum = String.valueOf(n);
System.out.println(myNum.length());
intcreator
  • 4,206
  • 4
  • 21
  • 39
0

You could simply go ahead by converting the integer into a char array.

int x = 20;
char[] chars = x.toCharArray();

Then you can get the length of the array.

int numberOfDigits = chars.length();

Hope it helps.

D3myon
  • 181
  • 1
  • 10
0

In case your teacher is tricky like I would be, this gives you the number of digits entered as well as the number of digits in the resulting int...

import java.util.Scanner;

public class StackOverflow_32898761 {

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        try
        {
            int n;

            System.out.println("Enter a positive integer: ");
            String value = input.next();

            try
            {
                n = Integer.parseInt(value);

                if (n < 0)
                {
                    System.out.println("I said a POSITIVE integer... [" + value + "]");
                }
                else
                {
                    System.out.println("You entered " + value.length() + " digits");
                    System.out.println("The resulting integer value has " + String.valueOf(n).length() + " digits");
                    System.out.println("The integer value is " + n);
                }           
            }
            catch (Exception e)
            {
                System.out.println("Invalid integer value [" + value + "]");
            }
        }
        finally
        {
            input.close();
        }
    }
}
Michael
  • 519
  • 4
  • 14