/* Johnny A. Shaieb * CS 7473 Network Security * Project 3 */ import java.text.NumberFormat; public class Sprintf { private String str; private int spacing; private String justify; public Sprintf(String s, int t) { str = s; spacing = t; justify = "p"; } public Sprintf(String sValue, int spaceValue, String strJustify) { str = sValue; spacing = spaceValue; justify = strJustify; } public Sprintf(int sValue, int spaceValue, String strJustify) { str = Integer.toString(sValue); spacing = spaceValue; justify = strJustify; } public Sprintf(long sValue, int spaceValue, String strJustify) { //str = Long.toString(sValue); str = sValue + ""; spacing = spaceValue; justify = strJustify; } public Sprintf(double sValue, int spaceValue, String strJustify) { str = Double.toString(sValue); spacing = spaceValue; justify = strJustify; } public Sprintf(Sprintf o) { str = o.str; spacing = o.spacing; justify = o.justify; } public String massPrint() { String line = ""; for(int i = 0; i < spacing; i++) { line = line + str; } return line; } public String formatIt() { int l = str.length(); String strFormat; if(l < spacing) { int diff = spacing - l; String spaces = ""; for(int k=1; k<=diff; k++) { spaces = spaces + " "; } if(justify.equalsIgnoreCase("l")) { strFormat = str + spaces; } else if(justify.equalsIgnoreCase("r")) { strFormat = spaces + str; } else { //if user value does not match l or r, r will be default strFormat = spaces + str; } } else { strFormat = str; } return strFormat; } public void sprintfDisplay() { System.out.println(formatIt()); } public String tabPrint() { String templine = ""; for(int i = 0; i < spacing; i++) { templine += '\t'; } return templine + str; } public static void main(String[] args) { System.out.println(new Sprintf("=", 20).massPrint()); } }