Advertisement
Guest User

FormattingNumbers

a guest
May 11th, 2014
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package javaSyntaxix;
  2.  
  3. import java.util.Scanner;
  4. public class FormattingNumbers {
  5.    
  6.     public static String padLeft(String s, int n) {
  7.         return String.format("%1$" + n + "s", s);  
  8.     }
  9.    
  10.     public static String padRight(String s, int n) {
  11.          return String.format("%1$-" + n + "s", s);  
  12.     }
  13.    
  14.     public static void main(String[] args) {
  15.         Scanner sc = new Scanner(System.in);
  16.         int first = Integer.parseInt(sc.next());
  17.         String hex = Integer.toHexString(first);
  18.         hex = hex.toUpperCase();
  19.         hex = padRight(hex,10-hex.length());
  20.         String binary = Integer.toBinaryString(first);
  21.         int binaryLength = binary.length();
  22.         int padCount = 10 - binaryLength;
  23.         String builder = "";
  24.         for(int i=0;i<padCount;i++) builder = builder + "0";
  25.         String paddedBinary = builder + binary;
  26.         String secondAsString = sc.next();  
  27.         secondAsString = String.format("%.2f",Double.parseDouble(secondAsString));
  28.         int secondLength = secondAsString.length();
  29.         padCount = 10 - secondLength;
  30.         builder = "";
  31.         for(int i=0;i<padCount;i++) builder = builder + " ";
  32.         secondAsString = builder + secondAsString;
  33.         String thirdAsString = sc.next();
  34.         thirdAsString = String.format("%.3f",Double.parseDouble(thirdAsString));
  35.         int thirdLength = thirdAsString.length();
  36.         padCount = 10 - thirdLength;
  37.         builder = "";
  38.         for(int i=0;i<padCount;i++) builder = builder + " ";
  39.         thirdAsString = thirdAsString + builder;
  40.         System.out.print("|");
  41.         System.out.print(hex);
  42.         System.out.print("|");
  43.         System.out.print(paddedBinary);
  44.         System.out.print("|");
  45.         System.out.print(secondAsString);
  46.         System.out.print("|");
  47.         System.out.print(thirdAsString);
  48.         System.out.print("|");     
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement