The Java code is right here to show.
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- class DemoLeft
- {
- public static void main(String[] arg) throws Exception
- {
- BufferedReader in = new BufferedReader(new InputStreamReader(System. in ));
- System.out.println("1-Left insertion with zeros at the start of ''Number''");
- System.out.println("2-Left insertion with zeros at the start of ''String''");
- System.out.println();
- System.out.print("Select any option: ");
- int select = Integer.parseInt( in .readLine());
- switch (select)
- {
- case 1:
- System.out.println("After insertion: " + ">" + padLeft(143, 14) + "<");
- break;
- case 2:
- String s = padLeft("Excellent", 14);
- System.out.println("After insertion: " + ">" + s.replace(" ", "0") + "<");
- break;
- default:
- System.out.println("Wrong choice");
- }
- }
- public static String padLeft(int s, int n)
- {
- return String.format("%0" + n + "d", s);
- }
- public static String padLeft(String s, int n)
- {
- return String.format("%0$" + n + "s", s);
- }
- }
Thank you, keep learning and sharing.