# Values of Input

• October 12th, 2009, 10:00 AM
chronoz13
Values of Input
I have a program that converts a numbers, using enums on switches

this is a bit too long..
Code :

```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

Code :

```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```
because the variable that will hold for the input value is STRING type..

but i want to make this similar to that.
Code :

```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
• October 12th, 2009, 10:02 AM
chronoz13
Re: Values of Input
need some big help with this...

i know that this is slightly complex with my level... but i dont want to STOP if theres a lot of question whirling around my mind... im moving so far with this program...and i dont want a liitle algorithm that is possible to make will STOP me to continue this... please help me..

I would really appreciate any help...
• October 12th, 2009, 08:46 PM
helloworld922
Re: Values of Input
Two ways: one is to input a string, then wait for the user to type something like quit, and another way is just tell the user to type quit, and catch the exception thrown (or not).

Code :

```Scanner input = new Scanner(System.in); String str = ""; System.out.println("Enter a number or quit: "); str = input.next(); while(!"quit".equals(input)) { int number = Integer.parseInt(str); // .. do stuff System.out.println("Enter a number or quit: "); str = input.next(); }```
• October 13th, 2009, 03:44 AM
chronoz13
Re: Values of Input
is this similar to the logic that you gave me helloworld?

Code :

```System.out.print("Enter The Decimal Number: "); keyIn = br.readLine();   if ((keyIn.equalsIgnoreCase("exit")) || (keyIn.equalsIgnoreCase("quit"))) {   System.out.println(""); System.out.println("Thank You!"); System.exit(0); } else {   decimalInput = Integer.parseInt(keyIn); }```

so i did that instead of this:
Code :

`keyIn = Integer.parseInt(br.readLine());`
Code :

`decimalInput = Integer.parseInt(br.readLine());`

"dividing the process of INPUT from the process of PARSING"?
• October 13th, 2009, 06:52 PM
helloworld922
Re: Values of Input
The reason you can't parse right away is because you don't know if the user typed in Quit/Exit. It takes a few steps to parse whatever the user typed, so it's better to do the number parsing later.
• November 7th, 2009, 08:31 PM
r2ro_serolf
Re: Values of Input
you can also use the JOptionPane..

located at the import.javax.swing.*; package
• November 7th, 2009, 08:36 PM
helloworld922
Re: Values of Input
Regardless of how he gets the input, he will still have to parse it.

JOptionPane will return a string just as Scanner will return a String. The only difference is that JOptionPane will return null if the window is closed without pressing the "Ok" button (or enter).
• November 7th, 2009, 11:34 PM
chronoz13
Re: Values of Input
Quote:

The only difference is that JOptionPane will return null if the window is closed without pressing the "Ok" button (or enter).

i want to add some question here... whats the purpose of "null"? in JOptionPane?
• November 8th, 2009, 12:08 AM
helloworld922
Re: Values of Input
Someone decided that's what it should return if the user doesn't OK the output :P

That's the only value that's available since technically an empty string "" is an allowed OK return value.
• November 8th, 2009, 02:35 AM
chronoz13
Re: Values of Input
ahhh so if i press "space bar" or "close" it... it automatically disappear... so thats the purpose of the "null" in the
Code :

`Component parentComponent,`
• November 8th, 2009, 02:46 AM
Json
Re: Values of Input
Yeah or if you press the cancel button :)

// Json