Results 1 to 13 of 13
- 04-08-2011, 06:17 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
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.
Thanks!Java Code:if (firstNumber == -1) { firstNumber = number; } else { if(operator != 0) { if(secondNumber == -1) { secondNumber = number; } else { secondNumber = secondNumber & number; } } else { firstNumber = firstNumber & number; } }
-
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.
- 04-08-2011, 06:27 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
Oh man, I didn't even think of that :(
-
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)
-
this is the code i've made, you can try to adapt it to your program if you want.
BUTTON ACTIONSJava 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); }
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.
- 04-08-2011, 07:04 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
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)); }
-
-
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.
-
I'm sure it does, and I'm not criticizing its functionality, just its maintainability.
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.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.
- 04-08-2011, 07:44 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
Fubarable,
Does the change to NULL values in my code look ok? Instead of using -1...
Thanks
-
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.
- 04-08-2011, 08:55 PM #12
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.Java Code:else { secondNumber = secondNumber & number; } } else { firstNumber = firstNumber & number; } }
- 04-09-2011, 07:05 PM #13
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
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
-
Complicated Draw
By Desdenova in forum New To JavaReplies: 9Last Post: 05-27-2010, 08:44 PM -
String replacement...
By diskhub in forum New To JavaReplies: 6Last Post: 05-19-2010, 04:20 AM -
Complicated Method
By Desmond in forum New To JavaReplies: 5Last Post: 03-17-2010, 11:31 AM -
writng event listners ( seems complicated)
By Basit56 in forum AWT / SwingReplies: 1Last Post: 08-25-2009, 09:11 AM -
Looking for JGroups replacement
By asynchrony in forum NetworkingReplies: 4Last Post: 10-17-2008, 10:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks