Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Nested 'If' Statements

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Nested 'If' Statements

    Hello again, all,

    I am in the process of creating a calculator GUI that calculates different answers based on inputs two main comboboxes and numbers in the appropriate textfields. The first one allows the user to choose from 18 different materials, while the second has the user choose between two different shapes. In a brief word explanation, here's how it's set up:

    User chooses material.
    User chooses shape.
    User types in appropriate values.
    'Calculate' button clicked.
    If shape = rectangle, execute rectangle calculation.
    Else if shape = cylinder, execute cylinder calculation.

    Everything works just fine with zero errors on all 18 materials and all kinds of decimal numbers when the second shape is selected. So the math and layout is solid. However, when the first shape is selected, it returns a $0.00 answer regardless of the input values (still no red-line errors). In efforts to troubleshoot, when I have
    /* Cylinder section */, the rectangle section works to a 'T'. This, I believe is caused by poor formatting in the syntax of the 'if' 'else if' in regards to the shape combobox. But, I'm not sure how to correctly format it to fix the issue. Here is a sample of the coding, if you need more I can provide it, but it's rather lengthy. Any help is much appreciated!

    //If 'Rectangle' is selected:
          if(shapeDropDown.getSelectedIndex()==1) {
          //6061
          if(materialDropDown.getSelectedIndex()==0) {
              String  msg0 = "The price is: $"
                  + currencyFormat.format((0.098*(number2*number3*number4)*3.06));
                    totalPrice.setText(msg0);               
          } 
           //2024
          if(materialDropDown.getSelectedIndex()==1) {
              String  msg1 = "The price is: $"
                  + currencyFormat.format((0.1*(number2*number3*number4)*5.61));
                    totalPrice.setText(msg1);
          }
     
    ...
     
     //If 'Cylinder' is selected:
        } else if(shapeDropDown.getSelectedIndex()==2); {
          //6063 
          if(materialDropDown.getSelectedIndex()==0){
               String  msg0c = "The price is: $"
                  + currencyFormat.format(((0.098*(((number1/2)*(number1/2))*3.14159*number2))*3.06));
                    totalPrice.setText(msg0c);}
           //2024
         if(materialDropDown.getSelectedIndex()==1){
               String  msg1c = "The price is: $"
                  + currencyFormat.format(((0.1*(((number1/2)*(number1/2))*3.14159*number2))*5.61));
                    totalPrice.setText(msg1c);} ...


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Nested 'If' Statements

    Isn't there a zero index for the shape selected dropdown? Show that code.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Nested 'If' Statements

    The zero index simply says "Choose Shape" and is not coded. As far as I can tell, it hasn't affected the rest of the program.

    I should put an error message in there if the end user leaves it like that, but holding off on that until I get this ironed out.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Nested 'If' Statements

    The cylinder if() statement has a semicolon at the end.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Cal S. (September 11th, 2014)

  6. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Nested 'If' Statements

    You've got to be kidding me...

    Wow. Newer to this than I thought!

    I figured it had to be something simple. Oy vey. It works now, thanks so much!

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,401
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Nested 'If' Statements

    There is no excuse for that. The IDE should give a warning. I use the -Xlint option with the javac command and get a warning:
    Running: D:\Java\jdk1.7.0_45\bin\javac.exe -cp . -Xlint TestCode18.java
     
    TestCode18.java:601: warning: [empty] empty statement after if
          if(1 > 2);
                   ^
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Nested 'If' Statements

    I dont even know why java allows stupid things like that. Sometimes one has to wonder what the people were thinking when they wrote the ruleset.

  9. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Nested 'If' Statements

    Quote Originally Posted by Cornix View Post
    I dont even know why java allows stupid things like that. Sometimes one has to wonder what the people were thinking when they wrote the ruleset.
    Yeah, there weren't any red lines or anything. I just skipped over it when writing it the first time and never saw it again.

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,401
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Nested 'If' Statements

    Add the -Xlint option to the javac command and you'll get a warning.
    Don't have your IDE suppress warnings.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Nested 'If' Statements

    Quote Originally Posted by Norm View Post
    Add the -Xlint option to the javac command and you'll get a warning.
    Don't have your IDE suppress warnings.
    Definitely keep that in mind for the future, thank you.

Similar Threads

  1. Questions about if statements and if-else statements
    By chakana101 in forum Java Theory & Questions
    Replies: 5
    Last Post: March 23rd, 2014, 05:37 PM
  2. Help my if statements!
    By l2code in forum Loops & Control Statements
    Replies: 1
    Last Post: March 22nd, 2013, 01:47 PM
  3. [SOLVED] If and else if statements???
    By skeptile2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 5th, 2013, 12:44 AM
  4. [SOLVED] Help with nested if statements.
    By joon in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 17th, 2011, 10:25 AM
  5. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM