Results 1 to 9 of 9
  1. #1
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default How would I make a condition in this code

    Here's the code of my program..

    Java Code:
    double sam  ; 
            try{
               
                      sam = Double.parseDouble(
                        this.insam.getText());
                
                
            } catch(Exception e){
                JOptionPane.showMessageDialog(this, "Re-enter Sample Size of Col A",
                        "Invalid Input", JOptionPane.ERROR_MESSAGE);
                return;
            }
    Now, I want to make an exception that is if "sam" is less than 30, an Error Message will come out... But more than 30 the value is accepted. I tried some condition, but It doesn't work... Please check it below

    Java Code:
     if (sam > 30){
        
                         sam = Double.parseDouble(this.insam.getText());
              
              }else{
                  JOptionPane.showMessageDialog(this, "Re-enter Sample Size of Col A", "Invalid Input", 
                          JOptionPane.ERROR_MESSAGE);
                  return;
              }
    thanks for your help...

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your problem is that you are only parsing the data if sam is 30, but you haven't initialized sam yet.

    Java Code:
    try{
      parse sam
      if(condition)
    
      end if
      else
    
      end else

  3. #3
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    did you tried this?
    Java Code:
    double sam  ; 
            try{
               
           if (sam > 30){
        
                         sam = Double.parseDouble(this.insam.getText());
              
              }else{
                  JOptionPane.showMessageDialog(this, "Re-enter Sample Size of Col A", "Invalid Input", 
                          JOptionPane.ERROR_MESSAGE);
                
              }
                
                
            } catch(Exception e){
                JOptionPane.showMessageDialog(this, "Re-enter Sample Size of Col A",
                        "Invalid Input", JOptionPane.ERROR_MESSAGE);
                return;
            }

  4. #4
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Oh thanks sunde887, I forgot to put it on the code, I've initialized it also, but haven't tried your code...

    UJJAL DHAR, thank you but I have tried that code already, it works, but the problem is even if greater than or less than, the error message will always pop out...

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Are you sure that error message isn't coming from the exception?
    You do, after all, have exactly the same error showing up, and no way to determine if it's due to an exception or simply down to sam being < 30.

  6. #6
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    @Tolls:
    Yes, there's an error coming from exception, that is if I enter a character not a number or simply living it blank, error will come out. But I want it also to come out when sam is less than 30.

  7. #7
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    I love you sunde887... It works now...! Thanks to all of you who help me on this code.... Thank you very much....

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I was just about to post and clarify what I meant a bit. I'm glad to have helped. Please mark your thread solved with the thread tools at the top if you are done.

  9. #9
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Help I need to make this code simpler ASAP!!
    By Quizzle23 in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 02:10 AM
  2. How to make EXE from code?
    By heyitsmebenjamin in forum New To Java
    Replies: 1
    Last Post: 01-09-2011, 06:37 PM
  3. Replies: 6
    Last Post: 04-06-2010, 11:02 AM
  4. Replies: 1
    Last Post: 03-22-2010, 08:21 AM
  5. how to make my code better
    By vendetta in forum New To Java
    Replies: 4
    Last Post: 02-10-2010, 09:14 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
  •