Home > front end >  How do you make it so that when you enter a number it puts a space between each integer
How do you make it so that when you enter a number it puts a space between each integer

Time:11-22

import java.util.Scanner;

public class Digits {

    public static void main(String[] args) {
        /*
         * 
    count = 1 
    temp = n 
    while (temp > 10) 
        Increment count. 
        Divide temp by 10.0. 
   */
        
        //Assignment: fix this code to print: 1 2 3 (for 123)
        //temp = 3426 -> 3 4 2 6
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int count = 1;
        int temp = input.nextInt();
        while(temp >= 10){
            count  ;
            temp = temp / 10;
            System.out.print(temp   " ");
        }
    }

}

Need help fixing code. Example: when you type 123 it becomes 1 2 3.

CodePudding user response:

Your code is dividing by ten each time, that could be used to print the value in reverse. To print it forward you need a bit more math involving logarithms. Sometime like,

Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int temp = input.nextInt();
while (temp > 0) {
    int p = (int) (Math.log(temp) / Math.log(10));
    int v = (int) (temp / Math.pow(10, p));
    System.out.print(v   " ");
    temp -= v * Math.pow(10, p);
}

Alternatively, read a line of input. Strip out all non digits and then print every character separated by a space. Like,

String temp = input.nextLine().replaceAll("\\D", "");
System.out.println(temp.replaceAll("(.)", "$1 "));

CodePudding user response:

Most of your code is correct, and what you are trying to do is divide by 10 and then print out the value - this probably should have been a modulus operation % to get the remainder of the operation and print that out - but a nice way of thinking about it.

Nevertheless.

You can just use a string and then split the string on each character

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");

        // we know that we are going to get some input - so we will just grab it as a String
        // whilst we are expecting an int - we will test this later.
        // we are doing this as it makes it easier to split the contents of a string
        String temp = input.next();

        // is this an int? - we will test this first
        try {
            // if this parsing fails - then it will throw a java.lang.NumberFormat exception 
            // see the catch block below
            int test = Integer.parseInt(temp);
            
            

            // at this point it is an int no exception was thrown- so let's go 
            // through and start printing out each character with a space after it

            // the temp(which is a string).toCharArray returns a char[] which we
            // can just iterate through and set the variable of each iteration to 'c'
            for (char c : temp.toCharArray()) {
                
                // now we are going to print out the character with a space after it
                System.out.print(c   " ");
            }

        } catch (NumberFormatException ex){
            // this is not an int as we got a number format exception...

            System.out.println("You did not enter an integer. :(");
        }

        // be nice and close the resource
        input.close();
    }

CodePudding user response:

Answering solely your question, you can use this one-line code.

int test = 123;
System.out.println(String.join(" ", Integer.toString(test).split("")));

Output is: 1 2 3

  • Related