Results 1 to 7 of 7

Thread: Help.

  1. #1
    Ideliveryx is offline Member
    Join Date
    Apr 2016
    Posts
    5
    Rep Power
    0

    Default Help.

    import java.util.Scanner;


    class Income{

    public static void main (String[] args)

    {

    String mStatus;
    int income;
    double tax;
    Scanner sc = new Scanner(System.in);
    mStatus = sc.nextLine();
    income = sc.nextInt();

    if (mStatus == "Single"){

    if (income <= 32000){

    tax = 0.1 * income;

    }else{

    tax = 3200 + 0.25 * (income - 32000);
    }
    }else if(income <= 64000){

    tax = 0.1 * income;
    }
    else{

    tax = 6400 + 0.25 * (income - 64000);
    }
    System.out.print(tax);

    }

    }

    What i expecting all the tax able to print out if i key in a different status. If i enter the status "Single", and the income is 28000. it will print out tax = 0.1 * income. If i enter the status "Single", and the income is 48000. it STILL print out tax = 0.1 * income. else if i enter "Married", and the income is 58000 it print out tax = 0.1 * income.

    The main concept i was expecting to print out was tax = 6400 + 0.25 * (income - 64000); or tax = 3200 + 0.25 * (income - 32000); if i key in the different status or income.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Help.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Please post the program's current output and add some comments saying what is wrong with it and show what the output should be.

    NOTE: The == operator does not work well with Strings. You should use the equals() method to compare the contents of Strings.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Ideliveryx is offline Member
    Join Date
    Apr 2016
    Posts
    5
    Rep Power
    0

    Default Re: Help.

    Java Code:
    import java.util.Scanner;
    
    class Income{
    
    public static void main (String[] args)
    
        {
    
             String mStatus; 
             int income;
             double tax;
             Scanner sc = new Scanner(System.in);
             mStatus = sc.nextLine(); // Input the status "Single"
             income = sc.nextInt(); // Input the income "41000"
    
                if (mStatus == "Single"){  // True
    
                     if (income <= 32000){ // False
    
                          tax = 0.1 * income;
    
                            }else{
    
                                  tax = 3200 + 0.25 * (income - 32000); // Expected to print this tax but unable to print. instead. it print tax = 0.1 * income
                            }
                     }else if(income <= 64000){
    
                         tax = 0.1 * income;
                  }
                  else{
    
                        tax = 6400 + 0.25 * (income - 64000);
                  }
                      System.out.print(tax);
    
           }
    
    }
    Java Code:
    import java.util.Scanner;
    
    class Income{
    
    public static void main (String[] args)
    
        {
    
             String mStatus; 
             int income;
             double tax;
             Scanner sc = new Scanner(System.in);
             mStatus = sc.nextLine(); // Input the status "Married"
             income = sc.nextInt(); // Input the income "65000"
    
                if (mStatus == "Single"){  // False
    
                     if (income <= 32000){ 
    
                          tax = 0.1 * income;
    
                            }else{
    
                                  tax = 3200 + 0.25 * (income - 32000); 
                            }
                     }else if(income <= 64000){ // False
    
                         tax = 0.1 * income;
                  }
                  else{
    
                        tax = 6400 + 0.25 * (income - 64000); // Expected to print this tax but unable to print. instead. it print tax = 0.1 * income
                  }
                      System.out.print(tax);
    
           }
    
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Help.

    NOTE: The == operator does not work well with Strings. You should use the equals() method to compare the contents of Strings.
    Use x.equals("XXXX") instead of x == "XXXX"
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Ideliveryx is offline Member
    Join Date
    Apr 2016
    Posts
    5
    Rep Power
    0

    Default Re: Help.

    Java Code:
    Main.java:17: error: cannot find symbol
    if (mStatus.equals(Single))
                       ^
      symbol:   variable Single
      location: class Income
    1 error
    I uses the scanner to input my status.
    Last edited by Ideliveryx; 06-02-2016 at 03:39 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Help.

    Main.java:17: error: cannot find symbol
    if (mStatus.equals(Single))
    ^
    symbol: variable Single
    The compiler can not find a definition for the variable: Single.

    If you want a String, you need to enclose it in "s like this: "Single" like was done at line 16 in your code.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Ideliveryx is offline Member
    Join Date
    Apr 2016
    Posts
    5
    Rep Power
    0

    Default Re: Help.

    Thank you. You're thee best.. i was going to edit that but there was some issue with the server..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •