Java program that add comma to a number between 1000 and 999999
Posted by Samath
Last Updated: February 10, 2021

Write a program that reads a number between 1,000 and 999,999 from the user and prints it with a comma separating the thousands.
Here is a sample dialog:
   Please enter an integer between 1000 and 999999: 23456
   23,456

Code:

import java.util.Scanner;

public class Main
{
        public static void main(String[] args) {
            
            Scanner input = new Scanner(System.in);
            String number;
            String postfix;
            String prefix;
            
            System.out.print("Integer between 1000 and 999999: ");
            number = input.next();
            
            input.close();
            
            postfix = number.substring(number.length() - 3);
            prefix = number.substring(0, number.length() - 3);
            
            System.out.println(prefix + "," + postfix);
    }
}