Results 1 to 13 of 13
  1. #1
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default replacement for complicated if else?

    Hi,

    Would there be a way to replace the following complicated if/else statement?

    It's for a calculator to see if the user typed in a number or not and whether or not the numbers should be combined...like hitting "1" and "2" to make 12.

    note that a value of -1 for a number means it hasn't been set yet.

    an operator value of 0 means an operator hasn't been pressed yet.

    Java Code:
    		if (firstNumber == -1) {
    			firstNumber = number;
    		} else {
    			if(operator != 0) {
    				if(secondNumber == -1) {
    					secondNumber = number;
    				} else {
    					secondNumber = secondNumber & number;
    				}
    			} else {
    				firstNumber = firstNumber & number;
    			}
    		}
    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What if you want to allow for negative numbers? Wouldn't you be better with a boolean rather than some artificial and dangerous numeric value? And you could always simply parse the String in a try/catch(NumberFormatException nfe) block to see if the String is a valid number.

  3. #3
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Oh man, I didn't even think of that :(

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    hey i've already made something similar, but what i've done is made the input a STRING, because the user can't type, they only have buttons to press (0-9 and '.' and 00 and CLEAR)

    So what happens is, whenever they press a button (to input a digit) the String is read

    if the string contains one '.' already, if you press '.' button again it will be ignored (not appended to the string)

    whenever you press a digit, the digit is appended to the string

    when you're done, the program reads the string and parses it to a Double with Double.parseString(String str)
    (if you allow the user to type from a keyboard you can use a DocumentListener to remove any unwanted characters from the String when they are typed and then set the textbox to the String)

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    this is the code i've made, you can try to adapt it to your program if you want.

    Java Code:
        public void keypadInputAction(int action) {
            int dotCount = keypadInputString.replaceAll("[^.]", "").length();
            int dotPos = keypadInputString.indexOf(".");
            int length = keypadInputString.length();
            boolean moreThan2DP = dotCount >= 1 && length - dotPos > 2;
            switch (action) {
                case 0: case 1: case 2:
                case 3: case 4: case 5:
                case 6: case 7: case 8:
                case 9:
                    if (!moreThan2DP) keypadInputString += action;
                    break;
                case 10:
                    if (dotCount < 1) keypadInputString += ".";
                    break;
                case 11:
                    keypadInputAction(0);
                    keypadInputAction(0);
                    break;
                case 12:
                    keypadInputString = "";
                    break;
            }
            keypadInput.setText(keypadInputString);
        }
    BUTTON ACTIONS
    Buttons 0-9:
    keypadInputAction(number);

    Button '.':
    keypadInputAction(10);

    Button '00':
    keypadInputAction(11);

    Button 'CLR':
    keypadInputAction(12);

    Global String
    String keypadInputString = " ";
    Last edited by ozzyman; 04-08-2011 at 07:00 PM.

  6. #6
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Thanks ozzyman, I'll take a look at that.

    I changed my code to this:

    Java Code:
    	 public void getNumbers(int number)
    	 {
    		if (firstNumber == null) {
    			firstNumber = number;
    		} else {
    			if(operator != 0) {
    				if(secondNumber == null) {
    					secondNumber = number;
    				} else {
    					secondNumber = secondNumber & number;
    				}
    			} else {
    				firstNumber = firstNumber & number;
    			}
    		}
    			resultsLabel.setText ("" + (firstNumber));
    	 }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ozzyman View Post
    this is the code i've made, you can try to adapt it to your program if you want.

    Java Code:
        public void keypadInputAction(int action) {
            int dotCount = keypadInputString.replaceAll("[^.]", "").length();
            int dotPos = keypadInputString.indexOf(".");
            int length = keypadInputString.length();
            boolean moreThan2DP = dotCount >= 1 && length - dotPos > 2;
            switch (action) {
                case 0: case 1: case 2:
                case 3: case 4: case 5:
                case 6: case 7: case 8:
                case 9:
                    if (!moreThan2DP) keypadInputString += action;
                    break;
                case 10:
                    if (dotCount < 1) keypadInputString += ".";
                    break;
                case 11:
                    keypadInputAction(0);
                    keypadInputAction(0);
                    break;
                case 12:
                    keypadInputString = "";
                    break;
            }
            keypadInput.setText(keypadInputString);
        }
    BUTTON ACTIONS
    Buttons 0-9:
    keypadInputAction(number);

    Button '.':
    keypadInputAction(10);

    Button '00':
    keypadInputAction(11);

    Button 'CLR':
    keypadInputAction(12);

    Global String
    String keypadInputString = " ";
    Suggestions: I'd get rid of the magic numbers and would not have the CLR button or any other non-numeric/decimal point button use the same action.

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Fubarable, that is my Keypad. its a simple keypad that only allows the user to enter MONEY via the buttons, and thats the only way keypadInputString can be edited. It works perfect for me.

    Also, I need the CLR button to allow the user to clear the string when they need to type new amount of money, and its for my personal pleasure of making the code easy to read that all of the keypad instructions are in one place, thats why i made it action #12. ofcourse i could make a separate method for this however my main GUI java file is already 6,000 lines long and its hard enough to find things with that tiny scrollbar in netbeans so i like to keep all dedicated sections in their own place.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ozzyman View Post
    Fubarable, that is my Keypad. its a simple keypad that only allows the user to enter MONEY via the buttons, and thats the only way keypadInputString can be edited. It works perfect for me.
    I'm sure it does, and I'm not criticizing its functionality, just its maintainability.


    Also, I need the CLR button to allow the user to clear the string when they need to type new amount of money, and its for my personal pleasure of making the code easy to read that all of the keypad instructions are in one place, thats why i made it action #12. ofcourse i could make a separate method for this however my main GUI java file is already 6,000 lines long and its hard enough to find things with that tiny scrollbar in netbeans so i like to keep all dedicated sections in their own place.
    I'm again not criticizing the functionality of the app, or saying that you shouldn't have a CLR button, just the maintainability, and towards that JButtons that have significantly different actions should have different listeners.

  10. #10
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Fubarable,

    Does the change to NULL values in my code look ok? Instead of using -1...

    Thanks

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    to be honest, i started writing this while i started learning java as a beginner. and i'm still not quite sure how to write the program in such a way that each function of the software i'm writing has its own java file. so the problem for me is, the GUI requires so many buttons on the main window (because its an EPOS software i'm creating), the way i find easiest to maintain and update my own methods is to give all buttons related to one function the same name, often the same method with a Switch to cater for an entire group of buttons methods, like what you've seen in my keypad code.

  12. #12
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Java Code:
    else {
    					secondNumber = secondNumber & number;
    				}
    			} else {
    				firstNumber = firstNumber & number;
    			}
    		}
    I'm almost positive that bitwise AND isn't what you're intending to do here. If you want to get 12 from the operands 1 and 2, you'll probably want to convert them to String and concatenate them.

  13. #13
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Ok, here is the code with the strings:

    Java Code:
    if (firstNumber == null) {
    			firstNumber = number;
    		} else {
    			if(operator != 0) {
    				if(secondNumber == null) {
    					secondNumber = number;
    				} else {
    					String t1 = "" + secondNumber;
    					String t2 = "" + number;
    					String t3 = t1 + t2;
    					secondNumber = Integer.parseInt(t3);
    				}
    			} else {
    				String s1 = "" + firstNumber;
    				String s2 = "" + number;
    				String s3 = s1 + s2;
    				firstNumber = Integer.parseInt(s3);
    			}
    		}
    			//System.out.println(firstNumber);
    			resultsLabel.setText ("" + (firstNumber));
    	 }

Similar Threads

  1. Complicated Draw
    By Desdenova in forum New To Java
    Replies: 9
    Last Post: 05-27-2010, 08:44 PM
  2. String replacement...
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-19-2010, 04:20 AM
  3. Complicated Method
    By Desmond in forum New To Java
    Replies: 5
    Last Post: 03-17-2010, 11:31 AM
  4. writng event listners ( seems complicated)
    By Basit56 in forum AWT / Swing
    Replies: 1
    Last Post: 08-25-2009, 09:11 AM
  5. Looking for JGroups replacement
    By asynchrony in forum Networking
    Replies: 4
    Last Post: 10-17-2008, 10:54 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
  •