Results 1 to 4 of 4
  1. #1
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

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

    Java Code:
    double number;
    
    try {
    		            number = Double.parseDouble(tfnumber.getText());
    		            } catch (NumberFormatException e) {
    		            	JOptionPane.showMessageDialog(null, "Invalid bet. Please re-enter your number!");
    		            }
    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;
    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");
    		            }
    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?

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

    Default Re: If statement is not working properly

    Think through the logic here:

    Java Code:
    if ( number < amount || number > 0)
    Then answer: when will this ever be false? A simple fix will correct this.

  3. #3
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default Re: If statement is not working properly

    Aww, haven't seen it >.<. By replacing || with && it's working as it should. Thank you.

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

    Default Re: If statement is not working properly

    Quote Originally Posted by Alpa View Post
    Aww, haven't seen it >.<. By replacing || with && it's working as it should. Thank you.
    Eggsactly! And you're welcome!

Similar Threads

  1. NetBeans IDE not working properly
    By farmer in forum NetBeans
    Replies: 2
    Last Post: 12-09-2011, 03:12 PM
  2. for loop not working properly
    By lbgladson in forum New To Java
    Replies: 8
    Last Post: 10-15-2011, 12:33 AM
  3. TCP/IP client not working properly
    By Chrillz in forum Networking
    Replies: 2
    Last Post: 07-20-2011, 07:39 PM
  4. Why this block is not working properly?
    By juhiswt in forum New To Java
    Replies: 2
    Last Post: 03-13-2011, 11:08 AM
  5. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 12:13 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
  •