Results 1 to 8 of 8
  1. #1
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    28
    Rep Power
    0

    Default Passing a Null Argument to overload Procedure

    Hello,

    I am trying to create a class to read data from the JOptionPane. I am trying to create a general procedure and then be able to overload it using the rest. Basically I just want to call the procedure readString with options and without options while verifying that the user entered the correct information.


    Java Code:
    public class BankIO {
    
        public static final Object[] mainMenu = {"1. Create Account", "2. Delete Account", "3. Update Account", "4. Display Information", "5. Quit"};
        public static final String APPLICATION_NAME = "Banking Pro";
        
        
        public static String readString(String prompt) {
            return readString(prompt, null);
        }
    
        public static String readString(String prompt, Object[] options) {
    
            boolean validEntry = false;
            String entry = "";
    
            while (!validEntry) {
    
                entry = (String) JOptionPane.showInputDialog(null, prompt, APPLICATION_NAME, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
    
                // Verify that the user did not press the cancel button.
                if (entry != null && !entry.equalsIgnoreCase("")) {
                    validEntry = true;
                }
            }
    
            // Return the value.
            return entry;
        }
    }
    Any Ideas? thanks a lot :)

  2. #2
    2teez is offline Member
    Join Date
    May 2015
    Posts
    6
    Rep Power
    0

    Default Re: Passing a Null Argument to overload Procedure

    Hi,

    why call the method
    PHP Code:
            public static String readString(String prompt, Object[] options) {
    in
    PHP Code:
     
        public static String readString(String prompt) {
            return readString(prompt, null); // note here?
        }
    Using a null type? When you already have an array variable called mainMenu?
    Last edited by 2teez; 05-24-2015 at 07:28 AM.

  3. #3
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    28
    Rep Power
    0

    Default Re: Passing a Null Argument to overload Procedure

    Hi 2teez,

    Thanks a lot for the reply

    Quote Originally Posted by 2teez View Post
    why call the method ...When you already have an array variable called mainMenu?
    I want to do this way so I can implement it both ways;
    1. Passing an array which will ultimately populate the selections.
    2. Not passing anything and allowing the user to enter a value.

    An option could be to repeat code but I am trying to not.

    Java Code:
    public class BankIO {
    
        public static final Object[] mainMenu = {"1. Create Account", "2. Delete Account", "3. Update Account", "4. Display Information", "5. Quit"};
        public static final String APPLICATION_NAME = "Banking Pro";
    
        public static String readString(String prompt) {
    
            boolean validEntry = false;
            String entry = "";
    
            while (!validEntry) {
    
                entry = (String) JOptionPane.showInputDialog(null, prompt, APPLICATION_NAME, JOptionPane.QUESTION_MESSAGE);
    
                // Verify that the user did not press the cancel button.
                if (entry != null && !entry.equalsIgnoreCase("")) {
                    validEntry = true;
                }
            }
    
            // Return the value.
            return entry;
        }
    
        public static String readString(String prompt, Object[] options) {
    
            boolean validEntry = false;
            String entry = "";
    
            while (!validEntry) {
    
                entry = (String) JOptionPane.showInputDialog(null, prompt, APPLICATION_NAME, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
    
                // Verify that the user did not press the cancel button.
                if (entry != null && !entry.equalsIgnoreCase("")) {
                    validEntry = true;
                }
            }
    
            // Return the value.
            return entry;
        }
    }
    Thanks again

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Passing a Null Argument to overload Procedure

    Having read and re-read this it is unclear to me how you are going to use this. Can you provide a compilable example of how you plan on using these two methods?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    28
    Rep Power
    0

    Default Re: Passing a Null Argument to overload Procedure

    Hello,

    This is the code for class BankIO. Note that I added a new method concatObject to better represent the implementation.

    Java Code:
    public class BankIO {
    
        public static final Object[] mainMenu = {"1. Create Account", "2. Delete Account", "3. Update Account", "4. Display Information", "5. Quit"};
        public static final String APPLICATION_NAME = "Banking Pro";
    
        public static String readString(String prompt) {
    
            boolean validEntry = false;
            String entry = "";
    
            while (!validEntry) {
    
                entry = (String) JOptionPane.showInputDialog(null, prompt, APPLICATION_NAME, JOptionPane.QUESTION_MESSAGE);
    
                // Verify that the user did not press the cancel button.
                if (entry != null && !entry.equalsIgnoreCase("")) {
                    validEntry = true;
                }
            }
    
            // Return the value.
            return entry;
        }
    
        public static String readString(String prompt, Object[] options) {
    
            boolean validEntry = false;
            String entry = "";
    
            while (!validEntry) {
    
                entry = (String) JOptionPane.showInputDialog(null, prompt, APPLICATION_NAME, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
    
                // Verify that the user did not press the cancel button.
                if (entry != null && !entry.equalsIgnoreCase("")) {
                    validEntry = true;
                }
            }
    
            // Return the value.
            return entry;
        }
        
         public static String concatObject(Object[] objs) {
    
            String str = "";
    
            for (Object item : objs) {
                str += (item + "\n");
            }
    
            return str + "\n";
        }
    }

    This is the code for a class named Runner which is the implementation class.

    Java Code:
    public class Runner {
        
        public static void main(String[] args) {
            
            //Print a menu with options
            System.out.println(BankIO.readString(BankIO.concatObject( BankIO.mainMenu), BankIO.mainMenu));
            
            // Read other data
            System.out.println(BankIO.readString("Enter your name."));       
            
        }    
    }
    I hope this makes thing clearer.

    Thanks :)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Passing a Null Argument to overload Procedure

    You could use varargs and you wouldn't need to overload the method.

    Also, the empty String doesn't require that you ignore case for comparisons; the intent of the code would however be better expressed by isEmpty(). Either way, it's good practice to trim() the String, unless a non-zero length of blanks (spaces) is an acceptable input.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Passing a Null Argument to overload Procedure

    OK, I see what you are trying to do. In your previous post you were implementing a sort of helper routine to make use of an existing method. But it doesn't save you much and clouds the overall logic of what you are trying to do. Why not just have one dialog for the menu and one for the user input. Call each something different than readString (i.e. give them meaningful names). If you will need to check for correct user input at other locations you could have a validate method which simply returns a boolean:

    e.g.
    Java Code:
    do {
           // something
    } while(validate(entry));
    
    public static boolean validate(String entry) {
        // return some boolean expression
    }
    Keep in mind that what appears to be suboptimal may not be that bad. And more important is that it helps to document what you are doing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    28
    Rep Power
    0

    Default Re: Passing a Null Argument to overload Procedure

    Hello,

    Thanks a lot for the help.

    @Darryl, I implemented your suggestion for the trim() and .isEmpty() I looked into the varargs but in my short experience and I could be totally wrong (I do not want to seem arrogant at all) I think it takes clarity away from the code.

    @Jim This section is just the tip of the iceberg. I moved the validation section inside the BankIO class because I will require a lot of information from the user and the whole cycle was turning into a nightmare.

    This whole thing is because I am trying to implement something my professor suggested last class where all the overloaded methods cascaded into each other.


    This is the class finished. I think this is ok for my class. I removed all unnecessary comments, variables , and methods to make it easier to read.

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package joptiontest;
    
    import java.text.DateFormat;
    import java.util.Date;
    import javax.swing.JOptionPane;
    
    
    public class BankIO {
    
        public static final Object[] mainMenu = {"1. Create Account", "2. Delete Account", "3. Update Account", "4. Display Information", "5. Quit"};
        public static final String APPLICATION_NAME = "Banking Pro Systems";
        public static final String DEFAULT_PROMPT = "Select an option: \n\n";
        public static final String WELCOME = "Welcome to Baking Pro Systems" + "\n"
                                             + "--------------------------------------------" + "\n";
    
        //Method to concatenate the objects from the arrays.
        //Will allow me to expand or shrink them if needed.
        private static String concatObject(Object[] objs) {
    
            String str = "";
    
            for (Object item : objs) {
                str += (item + "\n");
            }
    
            return str + "\n";
        }
    
        public static String readMainMenu() {
            return readString(concatObject(mainMenu), mainMenu);
        }
     
        //Procedure to get the information after displaying the main menu.
        //Implements readString
        public static String readMainMenu(boolean showWelcome) {
    
            String fullString;
    
            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
            Date today = new Date();
    
            // Create the full string to be displayed
            fullString = WELCOME + df.format(today) + "\n\n" + DEFAULT_PROMPT + concatObject(mainMenu);
            
            return readString(fullString, mainMenu);
        }
    
        
        //Another ersion of readstring in case I jus want to use the default message.
        public static String readString() {
            return readString(DEFAULT_PROMPT).trim();
        }
    
        
        //Read string no objects passed. This I use for user input.(they have to actually type.
        public static String readString(String prompt) {
    
            boolean validEntry = false;
            String entry = "";
    
            while (!validEntry) {
    
                entry =  (String) JOptionPane.showInputDialog(null, prompt, APPLICATION_NAME, JOptionPane.QUESTION_MESSAGE);            
                entry = entry.trim();
                
                // Verify that the user did not press the cancel button.
                if (entry != null && !entry.isEmpty()){
                    validEntry = true;
                }
            }
    
            // Return the value.
            return entry;
        }
    
       //Read string with objects passed (Ihave more than one menu, not just mainMenu
        public static String readString(String prompt, Object[] options) {
    
            boolean validEntry = false;
            String entry = "";
    
            while (!validEntry) {
    
                entry = (String) JOptionPane.showInputDialog(null, prompt, APPLICATION_NAME, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
    
                // Verify that the user did not press the cancel button.
                if (entry != null && !entry.equalsIgnoreCase("")) {
                    validEntry = true;
                }
            }
    
            // Return the value.
            return entry;
        }
        
        //Read and integer
        public static int readInt(String s) {
            return Integer.parseInt(readString(s));
        }
    
        //Read a double
        public static double readDouble(String s) {
            return Double.parseDouble(readString(s));
        }
    }

    Once again thanks a lot for your help and wisdom :)

Similar Threads

  1. Argument Passing
    By shavindra in forum New To Java
    Replies: 2
    Last Post: 01-05-2013, 09:24 AM
  2. passing an array as argument
    By samanesh in forum New To Java
    Replies: 1
    Last Post: 02-29-2012, 03:10 PM
  3. Passing method as argument
    By susieferrari in forum New To Java
    Replies: 26
    Last Post: 06-13-2011, 10:43 AM
  4. problems while passing the parameters to an oracle procedure Ref Cursor
    By maas in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-21-2011, 06:00 PM
  5. Replies: 1
    Last Post: 09-10-2010, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •