Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Values of Input

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Values of Input

    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

    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.
    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
    Last edited by chronoz13; October 12th, 2009 at 10:08 AM.


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Exclamation 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...
    Last edited by chronoz13; October 12th, 2009 at 10:09 AM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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).

    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();
    }

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Values of Input

    is this similar to the logic that you gave me helloworld?

    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:
    keyIn = Integer.parseInt(br.readLine());
    or instead of this:
    decimalInput = Integer.parseInt(br.readLine());

    "dividing the process of INPUT from the process of PARSING"?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  6. #6
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Values of Input

    you can also use the JOptionPane..

    located at the import.javax.swing.*; package

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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).

  8. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Values of Input

    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?

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Values of Input

    Someone decided that's what it should return if the user doesn't OK the output

    That's the only value that's available since technically an empty string "" is an allowed OK return value.

  10. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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
    Component parentComponent,

  11. #11
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Values of Input

    Yeah or if you press the cancel button

    // Json

Similar Threads

  1. Replies: 1
    Last Post: December 22nd, 2011, 09:55 AM
  2. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 02:38 AM
  3. How to share variable values amongst different classes?
    By igniteflow in forum Object Oriented Programming
    Replies: 8
    Last Post: August 20th, 2009, 08:53 AM
  4. Reading IEEE float values from files.
    By username9000 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 30th, 2009, 12:56 PM
  5. Java program to find the minimum and maximum values of input data
    By awake77 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 20th, 2008, 05:12 PM