I have a program that converts a numbers, using enums on switches
this is a bit too long..
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; } } }
as you can notice on my program.. there is always an OPTION to exit.. by Entering the String "exit" or "quit"
no matter the case is ...
PROBLEM:
if you will read my code .. at the part OF ToDecimal...and FromDecimal.
my input MUST be Integer..
what i want is i want to put a terminating value as "STRING" on that part ("exit" or "quit" to terminate the whole process)..similar to what i have on the preceeding statements... (enter exit if you want to terminate)
but my BIGGEST HARDEST thing that im thinking now is how am i going to that??
I overrided the methods of numerical conversion such as .decode() method , and i notice that... it ACCEPTS string values ......
so my concern is merely possible but definitely impossible on my state for now..
to make it clear heres some of the blocks on my code:
if you run the program.. this will appear
because the variable that will hold for the input value is STRING type..How Do You Want Numbers To Be Converted? To Decimal or FromDecimal? Just Enter [TD]-To Decimals, or [FD]-From Decimals: //so if I enter "exit" here, the program will terminate
but i want to make this similar to that.
How Do You Want Numbers To Be Converted? To Decimal or FromDecimal? Just Enter [TD]-To Decimals, or [FD]-From Decimals: fd What Number Format Do You Want Decimals To Be Converted Into? Just type [hexa] for HEXADECIMAL, [octal] for OCTAL, [binary] for BINARY: hexa Enter The Decimal Number: //HERE!! i want to put a terminating value here.. but not 'zero' (0)
i notice the logic of the .decode() method.. and it accepts string but I cant formulate the big idea on how to deal with this.
BOTTOM LINE:
on the part of the program that will ask the NUMBERS.. I WANT TO PUT A TERMINATING VALUE BUT NOT A NUMBER