Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaSyntaxix;
- import java.util.Scanner;
- public class FormattingNumbers {
- public static String padLeft(String s, int n) {
- return String.format("%1$" + n + "s", s);
- }
- public static String padRight(String s, int n) {
- return String.format("%1$-" + n + "s", s);
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int first = Integer.parseInt(sc.next());
- String hex = Integer.toHexString(first);
- hex = hex.toUpperCase();
- hex = padRight(hex,10-hex.length());
- String binary = Integer.toBinaryString(first);
- int binaryLength = binary.length();
- int padCount = 10 - binaryLength;
- String builder = "";
- for(int i=0;i<padCount;i++) builder = builder + "0";
- String paddedBinary = builder + binary;
- String secondAsString = sc.next();
- secondAsString = String.format("%.2f",Double.parseDouble(secondAsString));
- int secondLength = secondAsString.length();
- padCount = 10 - secondLength;
- builder = "";
- for(int i=0;i<padCount;i++) builder = builder + " ";
- secondAsString = builder + secondAsString;
- String thirdAsString = sc.next();
- thirdAsString = String.format("%.3f",Double.parseDouble(thirdAsString));
- int thirdLength = thirdAsString.length();
- padCount = 10 - thirdLength;
- builder = "";
- for(int i=0;i<padCount;i++) builder = builder + " ";
- thirdAsString = thirdAsString + builder;
- System.out.print("|");
- System.out.print(hex);
- System.out.print("|");
- System.out.print(paddedBinary);
- System.out.print("|");
- System.out.print(secondAsString);
- System.out.print("|");
- System.out.print(thirdAsString);
- System.out.print("|");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement