Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.76 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class NumberSystem {
  4.  
  5.     public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  6.     public static int category,input;
  7.     public static String reBin;
  8.     public static int arr[] = new int[1];
  9.    
  10.     public static void main(String[] xxx) throws IOException {
  11.         Category();
  12.         Cases();
  13.     }
  14.  
  15.     public static void Category() throws IOException
  16.     {
  17.         try {
  18.             System.out.println("Please Input a Value that Given Below\n\t1 - Binary\n\t2 - Octal\n\t3 - Decimal\n\t4 - Hexadecimal\n\t5 - Exit\n\nEnter a Value: ");
  19.             String a = in.readLine();
  20.             category = Integer.parseInt(a);
  21.             if(a.length() >= 2){
  22.                 Category();
  23.             }else {
  24.                 Cases();
  25.             }
  26.         }catch(NumberFormatException d)
  27.         {
  28.             System.out.println("Syntax Error, Please Re-Input");
  29.             Category();
  30.         }
  31.     }
  32.  
  33.     public static void Input() throws IOException
  34.     {
  35.             if(category == 4)
  36.             {
  37.                 try{
  38.                     System.out.print("Enter the value of your INPUT: ");
  39.                     reBin = in.readLine();
  40.                 }catch(NumberFormatException daison){
  41.                     System.out.println("Syntax Error, Please Re-Input");
  42.                     Input();
  43.                 }
  44.             }else{
  45.                 try{
  46.                     System.out.print("Enter the value of your INPUT: ");
  47.                     int a = Integer.parseInt(in.readLine());
  48.                     input = a;
  49.                     reBin = Integer.toString(a);
  50.                 }catch(NumberFormatException daison){
  51.                     System.out.println("Syntax Error, Please Re-Input");
  52.                     Input();
  53.                 }
  54.             }
  55.     }
  56.  
  57.     public static void Cases() throws IOException
  58.     {
  59.         switch(category)
  60.         {
  61.             case 1:
  62.             case 2:
  63.             case 3:
  64.             case 4:
  65.                     Input();
  66.                     if(category == 1){Binary();}
  67.                     else if(category == 2){Octal();}
  68.                     else if(category == 3) {Decimal();}
  69.                     else {Hex();}
  70.                     break;
  71.             case 5:
  72.                     System.exit(0);
  73.                     break;
  74.             default:
  75.                     try{
  76.                         System.out.println("Syntax Error, Please Re-Input");
  77.                         System.out.println("Please Input a Value that Given Below\n\t1 - Binary\n\t2 - Octal\n\t3 - Decimal\n\t4 - Hexadecimal\n\t5 - Exit\n\nEnter a Value: ");
  78.                         String a = in.readLine();
  79.                         category = Integer.parseInt(a);
  80.                         if(a.length() >= 2) Category();
  81.                     }catch(NumberFormatException daison) {
  82.                         System.out.println("Syntax Error, Please Re-Input");
  83.                         Cases();
  84.                     }
  85.                     break;
  86.         }
  87.     }
  88.  
  89.     public static void Binary() throws IOException
  90.     {
  91.         try{
  92.                 int a = Integer.parseInt(reBin, 2);
  93.                 String b = Integer.toOctalString(a);
  94.                 String c = Integer.toHexString(a).toUpperCase();
  95.                 PrintValues(0,1,1,1, "null",b,Integer.toString(a),c);
  96.                 ReTain();
  97.         }catch(NumberFormatException daison)
  98.         {
  99.                 System.out.print("Error!!, Please use 1 and 0\n");
  100.                 Cases();
  101.         }
  102.     }
  103.  
  104.     public static void Octal() throws IOException
  105.     {
  106.         try{
  107.                 int aaa = Integer.parseInt(reBin, 8);
  108.                 String bbb = Integer.toHexString(aaa).toUpperCase();
  109.                 String ccc = Integer.toBinaryString(aaa);
  110.                 PrintValues(1,0,1,1,ccc, "null",Integer.toString(aaa),bbb);
  111.                 ReTain();
  112.         }catch(NumberFormatException daison)
  113.         {
  114.                 System.out.print("Error!, Please do not use 8 and up!!\n");
  115.                 Cases();
  116.         }
  117.     }
  118.  
  119.     public static void Decimal() throws IOException
  120.     {
  121.         try{
  122.                 PrintValues(1,1,0,1, Integer.toBinaryString(input), Integer.toOctalString(input) , "null" , Integer.toHexString(input));
  123.                 ReTain();
  124.         }catch(NumberFormatException daison)
  125.         {
  126.                 System.out.print("Error!, please input a good amount!\n");
  127.                 Cases();
  128.         }
  129.     }
  130.  
  131.     public static void Hex() throws IOException
  132.     {
  133.         try{
  134.                 int aa = Integer.parseInt(reBin, 16);
  135.                 String bb = Integer.toOctalString(aa);
  136.                 String cc = Integer.toBinaryString(aa);
  137.                 PrintValues(1, 1, 1, 0, cc, bb, Integer.toString(aa), "null");
  138.                 ReTain();
  139.         }catch(NumberFormatException daison)
  140.         {
  141.                 System.out.print("There is an Error, Re-Input!!\n");
  142.                 Cases();
  143.         }
  144.     }
  145.  
  146.     public static void ReTain() throws IOException
  147.     {
  148.         System.out.print("Would you like to try Again? Y/N: ");
  149.         char trytry = in.readLine().charAt(0);
  150.  
  151.         if(trytry == 'y' || trytry == 'Y'){ Category(); Cases(); }
  152.         else if(trytry == 'n' || trytry == 'N'){ System.exit(1); }
  153.         else{ System.out.println("Your input is Wrong!!!"); ReTain();}
  154.     }
  155.  
  156.     public static void PrintValues(int w, int x, int y, int z, String val1, String val2, String val3, String val4)
  157.     {
  158.         if(w == 1)System.out.println("The Binary Value is: " + val1);
  159.         if(x == 1)System.out.println("The Octal Value is: " + val2);
  160.         if(y == 1)System.out.println("The Decimal Value is: " + val3);
  161.         if(z == 1)System.out.println("The Hex Value is: " + val4);
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement