Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class NumberSystem {
- public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- public static int category,input;
- public static String reBin;
- public static int arr[] = new int[1];
- public static void main(String[] xxx) throws IOException {
- Category();
- Cases();
- }
- public static void Category() throws IOException
- {
- try {
- 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: ");
- String a = in.readLine();
- category = Integer.parseInt(a);
- if(a.length() >= 2){
- Category();
- }else {
- Cases();
- }
- }catch(NumberFormatException d)
- {
- System.out.println("Syntax Error, Please Re-Input");
- Category();
- }
- }
- public static void Input() throws IOException
- {
- if(category == 4)
- {
- try{
- System.out.print("Enter the value of your INPUT: ");
- reBin = in.readLine();
- }catch(NumberFormatException daison){
- System.out.println("Syntax Error, Please Re-Input");
- Input();
- }
- }else{
- try{
- System.out.print("Enter the value of your INPUT: ");
- int a = Integer.parseInt(in.readLine());
- input = a;
- reBin = Integer.toString(a);
- }catch(NumberFormatException daison){
- System.out.println("Syntax Error, Please Re-Input");
- Input();
- }
- }
- }
- public static void Cases() throws IOException
- {
- switch(category)
- {
- case 1:
- case 2:
- case 3:
- case 4:
- Input();
- if(category == 1){Binary();}
- else if(category == 2){Octal();}
- else if(category == 3) {Decimal();}
- else {Hex();}
- break;
- case 5:
- System.exit(0);
- break;
- default:
- try{
- System.out.println("Syntax Error, Please Re-Input");
- 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: ");
- String a = in.readLine();
- category = Integer.parseInt(a);
- if(a.length() >= 2) Category();
- }catch(NumberFormatException daison) {
- System.out.println("Syntax Error, Please Re-Input");
- Cases();
- }
- break;
- }
- }
- public static void Binary() throws IOException
- {
- try{
- int a = Integer.parseInt(reBin, 2);
- String b = Integer.toOctalString(a);
- String c = Integer.toHexString(a).toUpperCase();
- PrintValues(0,1,1,1, "null",b,Integer.toString(a),c);
- ReTain();
- }catch(NumberFormatException daison)
- {
- System.out.print("Error!!, Please use 1 and 0\n");
- Cases();
- }
- }
- public static void Octal() throws IOException
- {
- try{
- int aaa = Integer.parseInt(reBin, 8);
- String bbb = Integer.toHexString(aaa).toUpperCase();
- String ccc = Integer.toBinaryString(aaa);
- PrintValues(1,0,1,1,ccc, "null",Integer.toString(aaa),bbb);
- ReTain();
- }catch(NumberFormatException daison)
- {
- System.out.print("Error!, Please do not use 8 and up!!\n");
- Cases();
- }
- }
- public static void Decimal() throws IOException
- {
- try{
- PrintValues(1,1,0,1, Integer.toBinaryString(input), Integer.toOctalString(input) , "null" , Integer.toHexString(input));
- ReTain();
- }catch(NumberFormatException daison)
- {
- System.out.print("Error!, please input a good amount!\n");
- Cases();
- }
- }
- public static void Hex() throws IOException
- {
- try{
- int aa = Integer.parseInt(reBin, 16);
- String bb = Integer.toOctalString(aa);
- String cc = Integer.toBinaryString(aa);
- PrintValues(1, 1, 1, 0, cc, bb, Integer.toString(aa), "null");
- ReTain();
- }catch(NumberFormatException daison)
- {
- System.out.print("There is an Error, Re-Input!!\n");
- Cases();
- }
- }
- public static void ReTain() throws IOException
- {
- System.out.print("Would you like to try Again? Y/N: ");
- char trytry = in.readLine().charAt(0);
- if(trytry == 'y' || trytry == 'Y'){ Category(); Cases(); }
- else if(trytry == 'n' || trytry == 'N'){ System.exit(1); }
- else{ System.out.println("Your input is Wrong!!!"); ReTain();}
- }
- public static void PrintValues(int w, int x, int y, int z, String val1, String val2, String val3, String val4)
- {
- if(w == 1)System.out.println("The Binary Value is: " + val1);
- if(x == 1)System.out.println("The Octal Value is: " + val2);
- if(y == 1)System.out.println("The Decimal Value is: " + val3);
- if(z == 1)System.out.println("The Hex Value is: " + val4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement