How do I display the individual integers from the scanner input stream for example if the user enters 500 I would like to display it as 5, 0, 0. Here is the code guys

import java.util.Scanner;
public class Digit {
    public static void main(String[] args) {
        int number, i = 0, num, digits;
        int[] digit;
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter the digit: ");
        try{
            number = input.nextInt();
            num = number;
            digits = 0;
            while(num > 0){
                digits = num % 10;
                digit = new int[]{digits};          
                num = num / 10;             
                i++;                    
            }
            System.out.println("The length of the given: "+number+ " is: "+i);
            System.out.println("The Digits are: "+digit+" ,");

        }
        catch(Exception exception){
            exception.printStackTrace();

        }
        finally{
            input.close();
        }

    }
}

But when I execute this code I am getting an error

$ javac Digit.java
Digit.java:20: error: variable digit might not have been initialized
                        System.out.println("The Digits are: "+digit+" ,");
                                                              ^
1 error

How I fix this ?

firstly, for digit, you don't need an array. and if you do use an array (or any other variable) don't initialize it within the while block. if it never runs, the variable won't be initialized, and thus not recognized.

anyway, your approach is a bit over the top. an easier approach would be to set the int into a String, then iterate over the chars, and add those all to a new String, separated by ','s.

Stultuske,
What do you mean by this:

an easier approach would be to set the int into a String, then iterate over the chars, and add those all to a new String, separated by ','s.

Did you mean that the variable in line number 5, int[] digit; should be changed to string array ?

I have changed the above code to look like the following

...
while(num > 0){
    digits = num % 10;
    digit = new int[num];
    digit = new int[]{digits};          
    num = num / 10;             
    i++;
    System.out.print("The Digits are: "+digit+",");
}
...

This is the output I got in the console,

$ javac Digit.java
$ java Digit
Please enter the digit: 800
The Digits are: [I@55f96302 ,The Digits are: [I@3d4eac69 ,The Digits are:  [I@42a57993 ,The length of the given 800 is: 3

But I want it to be displayed as something like this
The Digits are: 8,0,0.

Edited 1 Year Ago by Varunkrishna: Typos in the code

you are using way too much variables.
drop the digit, digits, ... you don't need al that.

read input in int.
convert int to String.
instantiate a new StringBuilder "" (or String)
iterate over each char in the original String
  - if String(Builder) isEmpty
      -> append ", " to String(Builder)
  - append char to String
end-iteration

print int (originalNumber) and length of the original String
print the new String(Builder) 's value

stultuske
I could not get the line number 4

iterate over each char in the original String

import java.util.Scanner;
public class Digit {
    public static void main(String[] args) {
        int number, i = 0, num, digits;
        String digit;
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter the digit: ");
        try{
            number = input.nextInt();
            num = number;
            digits = 0;
            while(num > 0){
                digits = num % 10;
                digit = Integer.toString(num);
                StringBuilder digitString = new StringBuilder();
                num = num / 10;             
                i++;
                if(digitString.toString().isEmpty()){
                    digitString.append(digit);
                    System.out.println("Digits are: "+digitString);

                }
            }
            System.out.println("The length of the given "+number+ " is: "+i);
            //System.out.println("The Digits are: "+digit+" ,");

        }
        catch(Exception exception){
            exception.printStackTrace();

        }
        finally{
            input.close();
        }

    }
}

And this is the output I am getting

$ java Digit
Please enter the digit: 654
Digits are: 654
Digits are: 65
Digits are: 6
The length of the given 654 is: 3

Why is it printing the digits two times ?

...
while(num > 0){
                digits = num % 10;
                digit = Integer.toString(num);
                StringBuilder digitString = new StringBuilder();
                num = num / 10;             
                i++;
                for(int j = 0; j < digit.toString().length(); j++)
                {
                    if(digitString.toString().isEmpty()){
                        digitString.append(digit);
                        //System.out.println("Digits are: "+digitString);

                    }
                    //digitString.append(digit);            
                    System.out.println("The digits are: "+digitString.charAt(j));

                }
            }



$ java Digit
Please enter the digit: 654
The digits are: 6
The digits are: 5
The digits are: 4
The digits are: 6
The digits are: 5
The digits are: 6
The length of the given 654 is: 3

it is not. if you look closely, you'll see that the second sequence is not the same as the first.
but, again, why are you making it so difficult? no division required. just transform it to a char array and print each element.

Okay you've spent a week on this, now consider the following code .. as @stiltuske proposed use a char array and then print its elements

import java.util.Scanner;
public class Digit {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter the digit: ");
        char[] numbers = ("" + input.nextInt()).toCharArray();
        for(int i = 0; i<numbers.length; i++){
            System.out.println(numbers[i]);
        }        
    }
}

Output:
Please enter the digit: 123

1
2
3

Edited 1 Year Ago by Slavi

Slavi,
Thank you so much for taking time to reply back to this thread, but I want something like this, If the user enters 12345 as the digit. The digits are 1,2,3,4,5 in the output.

stultuske

import java.util.Scanner;
public class Digit2 {
    public static void main(String[] args) {
        int number, i = 0, num, digits;
        String digit;
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter the digit: ");
        try{
            number = input.nextInt();
            num = number;
            digits = 0;
            digits = num % 10;
            digit = Integer.toString(num);
            StringBuilder digitString = new StringBuilder();
            for (int j = 0; j< digit.length(); j++){
                char numChar = digit.charAt(j);
                if(digitString.toString().isEmpty()){
                    digitString.append(", ");
                }
                System.out.println("The digits are: "+numChar); 
                digitString.append(numChar);                
            }               
            System.out.println("The length of the given "+number+ " is: "+digit.length());
            System.out.println("The string Builder value is: "+digitString);

        }
        catch(Exception exception){
            exception.printStackTrace();

        }
        finally{
            input.close();
        }

    }
}

I am somewhat close. Here is the output that I had got in the console.

$ java Digit2
Please enter the digit: 12345
The digits are: 1
The digits are: 2
The digits are: 3
The digits are: 4
The digits are: 5
The length of the given 12345 is: 5
The string Builder value is: , 12345

But why is it printing something like this The string Builder value is: , 12345 as the output ? What is wrong ?

Sorry I am not sure what do you mean, as it is now it will output each of those values, here's example of my terminal
daniwebDel.png

stultuske
Alas Finally I got what I needed thank you so much.

import java.util.Scanner;
public class Digit2 {
    public static void main(String[] args) {
        int number, i = 0, num, digits;
        String digit;
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter the digit: ");
        try{
            number = input.nextInt();
            num = number;
            digits = 0;
            digits = num % 10;
            digit = Integer.toString(num);
            StringBuilder digitString = new StringBuilder();
            for (int j = 0; j< digit.length(); j++){
                char numChar = digit.charAt(j);
                if(!digitString.toString().isEmpty()){
                    digitString.append(", ");
                }
                System.out.println("The digits are: "+numChar); 
                digitString.append(numChar);                
            }               
            System.out.println("The length of the given "+number+ " is: "+digit.length());
            System.out.println("The string Builder value is: "+digitString);

        }
        catch(Exception exception){
            exception.printStackTrace();

        }
        finally{
            input.close();
        }

    }
}

Output:

$ java Digit2
Please enter the digit: 12345
The digits are: 1
The digits are: 2
The digits are: 3
The digits are: 4
The digits are: 5
The length of the given 12345 is: 5
The string Builder value is: 1, 2, 3, 4, 5

But it is the same thing :D except that I was printing just the numbers without the string "The digits are: " and the length. If I add the string in the print and another line it becomes this and prints the same thing

import java.util.Scanner;
public class Digit {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter the digit: ");
        char[] numbers = ("" + input.nextInt()).toCharArray();
        for(int i = 0; i<numbers.length; i++){
            System.out.println("The digits are " + numbers[i]);
        }
        System.out.println("The length of the numbers entered by the user is: " + numbers.length);        
    }
}

when you run the program then ..

Please enter the digit: 12345
The digits are 1
The digits are 2
The digits are 3
The digits are 4
The digits are 5
The length of the numbers entered by the user is: 5

Edited 1 Year Ago by Slavi

Salvi
Thanks man. I have been trying to achive the line number 9(The string Builder value is: 1, 2, 3, 4, 5) of my output. :)

This article has been dead for over six months. Start a new discussion instead.