Results 1 to 4 of 4
- 02-04-2012, 08:32 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
If statement is not working properly
Hi,
I have a problem with my if-statement. It isn't working properly.
I created a JButton and a JTextField. I want the user to only enter numbers. If he enters a letter or nothing, a MessageDialog should appear. That's working completely fine with this code:
But now I want to add some additional restrictions. "number" has to be less than "amount" and is supposed to be greater than 0 (so negative numbers are not allowed). But it is not working. Please have a look at this snippet:Java Code:double number; try { number = Double.parseDouble(tfnumber.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Invalid bet. Please re-enter your number!"); }
If I enter a negative number or 0, "It's working!" appears although it shouldn't. How can I get rid of that and where's the problem?Java Code:double number; double amount; try { number = Double.parseDouble(tfnumber.getText()); if ( number < amount || number > 0){ JOptionPane.showMessageDialog(null, "It's working!"); } else { JOptionPane.showMessageDialog(null, "Invalid input! Your input was either negative or 0!"); } } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Invalid input! Your input wasn't a number"); }
-
Re: If statement is not working properly
Think through the logic here:
Then answer: when will this ever be false? A simple fix will correct this.Java Code:if ( number < amount || number > 0)
- 02-04-2012, 08:36 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
Re: If statement is not working properly
Aww, haven't seen it >.<. By replacing || with && it's working as it should. Thank you.
-
Similar Threads
-
NetBeans IDE not working properly
By farmer in forum NetBeansReplies: 2Last Post: 12-09-2011, 03:12 PM -
for loop not working properly
By lbgladson in forum New To JavaReplies: 8Last Post: 10-15-2011, 12:33 AM -
TCP/IP client not working properly
By Chrillz in forum NetworkingReplies: 2Last Post: 07-20-2011, 07:39 PM -
Why this block is not working properly?
By juhiswt in forum New To JavaReplies: 2Last Post: 03-13-2011, 11:08 AM -
Log4j not working properly....
By prakash_dev in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 12:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks