Here is the Java code.
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Demo
{
public static void main(String[] arg) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System. in ));
System.out.println("1-Left insertion with space at the 'start' of ''Number''");
System.out.println("2-Left insertion with space at the 'start' of ''String''");
System.out.println("3-Right insertion with space at the 'end' of ''Number''");
System.out.println("4-Right insertion with space at the 'end' of ''String''");
System.out.println("5-Right insertion with zeros at the 'end' 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 s1 = padLeft("Excellent", 14);
System.out.println("After insertion: " + ">" + s1 + "<");
break;
case 3:
System.out.println("After insertion: " + ">" + padRight(7321, 14) + "<");
break;
case 4:
String s2 = padRight("Awesome", 14);
System.out.println("After insertion: " + ">" + s2 + "<");
break;
case 5:
String s3 = padRight1("Shift", 14);
System.out.println("After insertion: " + ">" + s3.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);
}
public static String padRight(int s, int n)
{
return String.format("%0$-" + n + "d", s);
}
public static String padRight(String s, int n)
{
return String.format("%0$-" + n + "s", s);
}
public static String padRight1(String s, int n)
{
return String.format("%0$-" + n + "s", s);
}
}Thank you, keep learning and sharing