public class NumericalConversion2_UF_CC {
private static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
private static enum Conversion {
TODECIMAL, FROMDECIMAL
}
private static enum ToDecimal {
HEXA, OCTAL, BINARY
}
private static enum FromDecimal {
HEXA, OCTAL, BINARY
}
public static void main(String[] args) throws IOException {
do {
Conversion choice = null;
String conversion;
System.out.println("How Do You Want Numbers To Be Converted? " +
"To Decimal or FromDecimal?");
System.out.print("Just Enter [TD]-To Decimals, or [FD]-From Decimals: ");
conversion = br.readLine();
if ((conversion.equalsIgnoreCase("fd") || (conversion.equalsIgnoreCase("td")))) {
System.out.print("");
}
else if ((conversion.equalsIgnoreCase("exit")) || (conversion.equalsIgnoreCase("quit"))) {
System.out.println("");
System.out.println("Thank You!");
System.exit(0);
}
else {
System.out.println("");
}
do {
if ((!conversion.equalsIgnoreCase("td")) && (!conversion.equalsIgnoreCase("fd"))) {
System.out.print("Just Enter [TD]-To Decimals, or [FD]-From Decimals: ");
conversion = br.readLine();
}
/**
* Terminating string is "exit" or "EXIT"
*/
if ((conversion.equalsIgnoreCase("exit")) || (conversion.equalsIgnoreCase("quit"))) {
System.out.println("Thank You!");
System.exit(0);
}
if (conversion.equalsIgnoreCase("fd")) {
choice = Conversion.FROMDECIMAL;
}
else if (conversion.equalsIgnoreCase("td")) {
choice = Conversion.TODECIMAL;
}
}
while ((!conversion.equalsIgnoreCase("td")) && (!conversion.equalsIgnoreCase("fd")));
switch (choice) {
case FROMDECIMAL:
do {
FromDecimal fd = null;
String fdInput;
int decimalInput;
System.out.println("");
System.out.println("What Number Format Do You Want Decimals To Be Converted Into?");
System.out.print("Just type [hexa] for HEXADECIMAL, [octal] for OCTAL, " +
"[binary] for BINARY: ");
fdInput = br.readLine();
System.out.println("");
if ((fdInput.equalsIgnoreCase("exit")) ||(fdInput.equalsIgnoreCase("quit"))) {
System.out.println("Thank You!");
System.exit(0);
}
do {
if ((!fdInput.equalsIgnoreCase("hexa")) && (!fdInput.equalsIgnoreCase("octal"))
&& (!fdInput.equalsIgnoreCase("binary"))) {
System.out.print("Just type [hexa] for HEXADECIMAL, [octal] for OCTAL, " +
"[binary] for BINARY: ");
fdInput = br.readLine();
if ((fdInput.equalsIgnoreCase("hexa")) || (fdInput.equalsIgnoreCase("octal"))
|| (fdInput.equalsIgnoreCase("binary"))) {
System.out.println("");
}
else {
System.out.print("");
}
}
if (fdInput.equalsIgnoreCase("hexa")) {
fd = FromDecimal.HEXA;
}
else if (fdInput.equalsIgnoreCase("octal")) {
fd = FromDecimal.OCTAL;
}
else if (fdInput.equalsIgnoreCase("binary")) {
fd = FromDecimal.BINARY;
}
else if ((fdInput.equalsIgnoreCase("exit")) || (fdInput.equalsIgnoreCase("quit"))) {
System.out.println("Thank You!");
System.exit(0);
}
}
while ((!fdInput.equalsIgnoreCase("hexa")) && (!fdInput.equalsIgnoreCase("octal"))
&& (!fdInput.equalsIgnoreCase("binary")));
do {
switch (fd) {
case HEXA:
System.out.print("Enter The Decimal Number: ");
decimalInput = Integer.parseInt(br.readLine());
System.out.println("Hexadecimal Is: " + Long.toHexString(decimalInput).toUpperCase());
break;
case OCTAL:
System.out.print("Enter The Decimal Number: ");
decimalInput = Integer.parseInt(br.readLine());
System.out.println("Octal Is: " + Long.toOctalString(decimalInput));
break;
case BINARY:
System.out.print("Enter The Decimal Number: ");
decimalInput = Integer.parseInt(br.readLine());
System.out.println("Binary Is: " + Long.toBinaryString(decimalInput));
break;
default:
System.out.println("There Are Only Three Numerical Conversion" +
"available");
break;
}
}
while (FdConvertCont());
}
while (FromDecimalCont());
break;
case TODECIMAL:
ToDecimal td = null;
String tdInput;
String hexaInput,
octalInput,
binaryInput;
do {
System.out.println("");
System.out.println("What Type Of Numerical Value You Want To Convert To Decimal?");
System.out.print("Just type [hexa] for HEXADECIMAL, [octal] for OCTAL, " +
"[binary] for BINARY: ");
tdInput = br.readLine();
System.out.println("");
if ((tdInput.equalsIgnoreCase("exit")) || (tdInput.equalsIgnoreCase("quit"))) {
System.out.println("Thank You!");
System.exit(0);
}
do {
if ((!tdInput.equalsIgnoreCase("hexa")) && ((!tdInput.equalsIgnoreCase("octal")))
&& ((!tdInput.equalsIgnoreCase("binary")))) {
System.out.print("Just type [hexa] for HEXADECIMAL, [octal] for OCTAL, " +
"[binary] for BINARY: ");
tdInput = br.readLine();
if ((tdInput.equalsIgnoreCase("hexa")) || (tdInput.equalsIgnoreCase("octa"))
|| (tdInput.equalsIgnoreCase("binary"))) {
System.out.println("");
}
else {
System.out.print("");
}
}
if (tdInput.equalsIgnoreCase("hexa")) {
td = ToDecimal.HEXA;
}
else if (tdInput.equalsIgnoreCase("octal")) {
td = ToDecimal.OCTAL;
}
else if (tdInput.equalsIgnoreCase("binary")) {
td = ToDecimal.BINARY;
}
else if ((tdInput.equalsIgnoreCase("exit")) || (tdInput.equalsIgnoreCase("quit"))) {
System.out.println("Thank You!");
System.exit(0);
}
}
while ((!tdInput.equalsIgnoreCase("hexa")) && (!tdInput.equalsIgnoreCase("octal"))
&& (!tdInput.equalsIgnoreCase("binary")));
do {
switch (td) {
case HEXA:
System.out.print("Enter The Hexadecimal Number: ");
hexaInput = br.readLine();
System.out.println("Hexadecimal to Decimal Is: " + Long.decode("#" + hexaInput));
break;
case OCTAL:
System.out.print("Enter The Octal Number: ");
octalInput = br.readLine();
System.out.println("Octal To Decimal Is: " + Long.decode("0" + octalInput));
break;
case BINARY:
System.out.print("Enter The Binary Code: ");
binaryInput = br.readLine();
System.out.println("Binary To Decimal Is: " + Long.parseLong(binaryInput, 2));
break;
default:
System.out.println("There Are Only Three Numerical Conversion" +
"available");
break;
}
}
while(TdConvertCont());
}
while (ToDecimalCont());
break;
}
}
while (ConversionCont());
}
/**
* This will ask the user if the wants to change the designated numerical type that
* he wants to convert the decimal.
*
* @return boolean value
* @throws IOException
*/
public static boolean FromDecimalCont() throws IOException {
String keyIn;
System.out.println("");
System.out.print("Do You Want To Change The Numerical Type?: ");
keyIn = br.readLine();
if (keyIn.equalsIgnoreCase("Y") || (keyIn.equalsIgnoreCase("YES"))) {
return true;
}
else if ((keyIn.equalsIgnoreCase("N")) || (keyIn.equalsIgnoreCase("NO"))) {
return false;
}
else {
return false;
}
}
/**
* This will ask the user if he wants to change the numerical type of the
* number that he wants to convert into decimal.
*
* @return boolean value
* @throws IOException
*/
public static boolean ToDecimalCont() throws IOException {
String keyIn;
System.out.println("");
System.out.print("Do You Want To Change The Numerical Type?: ");
keyIn = br.readLine();
if ((keyIn.equalsIgnoreCase("Y")) || (keyIn.equalsIgnoreCase("YES"))) {
return true;
}
else if ((keyIn.equalsIgnoreCase("N")) || (keyIn.equalsIgnoreCase("NO"))) {
return false;
}
else {
return false;
}
}
/**
* This will ask the user if he/she still wants to continue converting
*
* @return boolean value
* @throws IOException
*/
public static boolean ConversionCont() throws IOException {
String keyIn;
System.out.println("");
System.out.print("Continue Converting?: ");
keyIn = br.readLine();
if ((keyIn.equalsIgnoreCase("Y")) || (keyIn.equalsIgnoreCase("YES"))) {
System.out.println("");
return true;
}
else if ((keyIn.equalsIgnoreCase("N")) || (keyIn.equalsIgnoreCase("NO"))) {
System.out.println("");
System.out.println("Thank You And Have A Nice Day!");
return false;
}
else if ((keyIn.equalsIgnoreCase("exit")) || (keyIn.equalsIgnoreCase("quit"))) {
System.out.println("");
System.out.println("Thank You And Have A Nice Day!");
System.exit(0);
return false;
}
else {
return false;
}
}
/**
* This will ask the user if he/she still wants to continue converting
* according to the numerical type that has been chosen.
*
* @return boolean value
* @throws IOException
*/
public static boolean TdConvertCont() throws IOException {
String keyIn;
System.out.println("");
System.out.print("Convert Again?: ");
keyIn = br.readLine();
if ((keyIn.equalsIgnoreCase("Y")) || (keyIn.equalsIgnoreCase("YES"))) {
System.out.println("");
return true;
}
else if ((keyIn.equalsIgnoreCase("N")) || (keyIn.equalsIgnoreCase("NO"))) {
return false;
}
else if ((keyIn.equalsIgnoreCase("exit")) || (keyIn.equalsIgnoreCase("quit"))) {
System.out.println("");
System.out.println("Thank You!");
System.exit(0);
return false;
}
else {
return false;
}
}
/**
*
* @return boolean value
* @throws IOException
*/
public static boolean FdConvertCont() throws IOException {
String keyIn;
System.out.println("");
System.out.print("Convert Again?: ");
keyIn = br.readLine();
if ((keyIn.equalsIgnoreCase("Y")) || (keyIn.equalsIgnoreCase("YES"))) {
System.out.println("");
return true;
}
else if ((keyIn.equalsIgnoreCase("N")) || (keyIn.equalsIgnoreCase("NO"))) {
return false;
}
else if ((keyIn.equalsIgnoreCase("exit")) || (keyIn.equalsIgnoreCase("quit"))) {
System.out.println("");
System.out.println("Thank You!");
System.exit(0);
return false;
}
else {
return false;
}
}
}