if-else statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnise78
    New Member
    • Sep 2007
    • 2

    #1

    if-else statement

    I have looked over this program numerous of times and cant seem to get it running, can some one please guide me in the right direction.
    My error information is AnnualSalary.ja va:33: ' ) ' expected if (AnnualSalary <= 15,000) {


    [CODE=java] /*
    * AnnualSalary.ja va
    *
    * Created on September 18, 2007, 9:56 AM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package AnnualSalary;

    /**
    *
    * @author felde301
    */
    import javax.swing.JOp tionPane;

    public class AnnualSalary {

    /** Creates a new instance of AnnualSalary */
    public AnnualSalary() {
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    JOption.showInp utDialog (null,"Enter an annual salary:",
    JOptionPane.QUE STION_MESSAGE);



    if (AnnualSalary <= 15,000){

    taxratecategory = '1';

    }else if (AnnualSalary <= 15,000 - 25,000)

    {

    taxratecategory = '2';

    }else if (AnnualSalary => 25,001 - 40,000)
    {

    taxratecategory = '3';

    }else if (AnnualSalary => 40,0001 - 65,000)
    {

    taxratecategory = '4';
    }else if (AnnualSalary => 65,001 - 80,000)
    {

    taxratecategory = '5';

    }else if (AnnualSalary => 80,000)

    taxratecategory = '6';

    //end if

    //display the result

    JOptionPane.sho wMessageDialog( null, "The tax rate category for an annual salary");


    }

    }[/CODE]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by johnise78
    I have looked over this program numerous of times and cant seem to get it running, can some one please guide me in the right direction.
    My error information is AnnualSalary.ja va:33: ' ) ' expected if (AnnualSalary <= 15,000) {


    /*
    * AnnualSalary.ja va
    *
    * Created on September 18, 2007, 9:56 AM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package AnnualSalary;

    /**
    *
    * @author felde301
    */
    import javax.swing.JOp tionPane;

    public class AnnualSalary {

    /** Creates a new instance of AnnualSalary */
    public AnnualSalary() {
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    JOption.showInp utDialog (null,"Enter an annual salary:",
    JOptionPane.QUE STION_MESSAGE);



    if (AnnualSalary <= 15,000){

    taxratecategory = '1';

    }else if (AnnualSalary <= 15,000 - 25,000)

    {

    taxratecategory = '2';

    }else if (AnnualSalary => 25,001 - 40,000)
    {

    taxratecategory = '3';

    }else if (AnnualSalary => 40,0001 - 65,000)
    {

    taxratecategory = '4';
    }else if (AnnualSalary => 65,001 - 80,000)
    {

    taxratecategory = '5';

    }else if (AnnualSalary => 80,000)

    taxratecategory = '6';

    //end if

    //display the result

    JOptionPane.sho wMessageDialog( null, "The tax rate category for an annual salary");


    }

    }
    1.) Use code tags
    2.) Remove all those commas from the amounts.

    Edit: And now that I can see the code better under code tags, => is not a valid operator. Use >= instead.
    You have to declare variables before you can use them and each variable should have a type.

    Comment

    • johnise78
      New Member
      • Sep 2007
      • 2

      #3
      Thanks for you help I was able to get it running but am not getting the correct output. I should be able to enter a salary and it should tell me waht tax catergory am in.

      Output should look like this

      Enter an annual salary (ex...40350) => 34400

      The tax rate category for an annual salary of $34400 is 3

      [CODE=java] /*
      * AnnualSalary.ja va
      *
      * Created on September 18, 2007, 9:56 AM
      *
      * To change this template, choose Tools | Template Manager
      * and open the template in the editor.
      */

      package annualsalary;

      /**
      *
      * @author felde301
      */
      import javax.swing.JOp tionPane;

      public class AnnualSalary {

      /** Creates a new instance of AnnualSalary */
      public AnnualSalary() {
      }

      /**
      * @param args the command line arguments
      */
      public static void main(String[] args) {

      float annualsalary = 0;
      float taxratecategory = 0;

      JOptionPane.sho wInputDialog (null,"Enter an annual salary:",
      JOptionPane.QUE STION_MESSAGE);



      if (annualsalary <= 15000){

      taxratecategory = '1';

      }else if (annualsalary <= 15000 - 25000)

      {

      taxratecategory = '2';

      }else if (annualsalary <= 25001 - 40000)
      {

      taxratecategory = '3';

      }else if (annualsalary <= 400001 - 65000)
      {

      taxratecategory = '4';
      }else if (annualsalary <= 65001 - 80000)
      {

      taxratecategory = '5';

      }else if (annualsalary >= 80000)

      taxratecategory = '6';

      //end if

      //display the result

      JOptionPane.sho wMessageDialog( null, "The tax rate category for an annual salary");


      }

      }[/CODE]
      Last edited by r035198x; Sep 20 '07, 07:14 PM. Reason: added code tags

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        You forgot the code tags again. You have to read what's being replied to you to be able to get the most out of these forums.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by johnise78
          [CODE=java]
          float annualsalary = 0;
          JOptionPane.sho wInputDialog (null,"Enter an annual salary:",
          JOptionPane.QUE STION_MESSAGE);
          if (annualsalary <= 15000){
          ...
          }[/CODE]
          How is the JOptionPane to know that what you enter there has to be automagically
          stuffed into your 'annualsalary' variable? Computers aren't psychic you know.

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by JosAH
            .. Computers aren't psychic you know.

            kind regards,

            Jos
            Turing would be disappointed by that statement of course.
            Quite an exotic bit of code that by the OP.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by r035198x
              Turing would be disappointed by that statement of course.
              Nope, Alan Turing already knew that computers are soooo stupid. Even more:
              there were no computers in those days; Alan was a clever dicky.

              kind regards,

              Jos ;-)

              Comment

              Working...