6 Replies - 249 Views - Last Post: 01 February 2013 - 06:58 PM Rate Topic: -----

#1 Vato116  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-October 12

Code runs, but not everything prints.

Posted 01 February 2013 - 05:31 PM

My code runs without error but only the income and marital status are printed.

import java.util.*;
public class TaxSystem3 {
    
    public static void main(String[] args) {
    String status,m = null,s = null;
         int income;
// Prompt user to enter if married or single
    Scanner in = new Scanner (System.in);
    System.out.print("Enter M if married, or S if single: ");
    status = in.next();
    System.out.println();

//promt the user to enter income

   
    System.out.print("Enter income:");
    income = in.nextInt();
    System.out.println();
   
 // calculate amount taxed
    if ( status.equals(m)) {

       if (income <=8000)
            {
            System.out.println("Tax Paid: " +income * 0.1);
            }
            else if (income <=32000)
            {
            System.out.println("Tax Paid: " +800 * 0.02);
            }
            else if (income >32000);
            {
            System.out.println("Tax Paid: " +4400  * 0.03);
            }

if (status.equals(s)) {
    
      if  (income <=16000)
            {
            System.out.println("Tax Paid: " +income * 0.1);
            }
            else if (income <=64000)
            {
            System.out.println("Tax Paid: " +1600 * 0.15);
            }
            else if (income >64000)
            {
            System.out.println("Tax Paid: " +8800 * 0.25);
            }
            else
            {
            System.out.println("invalid entry!");
            }
        }
}
}//main


This post has been edited by macosxnerd101: 01 February 2013 - 05:35 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Code runs, but not everything prints.

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Code runs, but not everything prints.

Posted 01 February 2013 - 06:01 PM

Can you please give the exact output?

Nevermind, get rid of the variables m and s, and replace there usage with "m" and "s"
Was This Post Helpful? 1
  • +
  • -

#3 Vato116  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-October 12

Re: Code runs, but not everything prints.

Posted 01 February 2013 - 06:08 PM

I'm not sure if i understand what you mean. The output is:

Enter M if married, or S if single: m

Enter income:8000


Process completed.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3479
  • View blog
  • Posts: 11,820
  • Joined: 12-December 12

Re: Code runs, but not everything prints.

Posted 01 February 2013 - 06:12 PM

This if statement:
if (status.equals(s)) {

is nested within the other if and so will only be run if the status is m. But, of course, the second if will not happen because the status is m. It would be easier to recognise this flaw if the code were correctly indented.

Added: Actually, neither ifs will run as the status won't be null.

As indicated, you should be checking against the string values "m" and "s", as input by the user, and not null.

This post has been edited by andrewsw: 01 February 2013 - 06:18 PM

Was This Post Helpful? 1
  • +
  • -

#5 Vato116  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-October 12

Re: Code runs, but not everything prints.

Posted 01 February 2013 - 06:26 PM

I changed it to this, and it is pointing to the m and s in the if statement and saying it is an incompatible type. This is my first time with java, not sure i wrote it how you guys wanted.

import java.util.*;
public class TaxSystem3 {
    
    public static void main(String[] args) {
   String m;
   String s;
         int income;
// Prompt user to enter if married or single
    Scanner in = new Scanner (System.in);
    System.out.print("Enter M if married, or S if single: ");
    System.out.println();

//promt the user to enter income

   
    System.out.print("Enter income:");
    income = in.nextInt();
    System.out.println();
   
 // calculate amount taxed
    if (m) {

       if (income <=8000)
            {
            System.out.println("Tax Paid: " +income * 0.1);
            }
            else if (income <=32000)
            {
            System.out.println("Tax Paid: " +800 * 0.02);
            }
            else if (income >32000);
            {
            System.out.println("Tax Paid: " +4400  * 0.03);
            }

if (s) {
    
      if  (income <=16000)
            {
            System.out.println("Tax Paid: " +income * 0.1);
            }
            else if (income <=64000)
            {
            System.out.println("Tax Paid: " +1600 * 0.15);
            }
            else if (income >64000)
            {
            System.out.println("Tax Paid: " +8800 * 0.25);
            }
            else
            {
            System.out.println("invalid entry!");
            }
        }
    }
    }
}//main



Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3479
  • View blog
  • Posts: 11,820
  • Joined: 12-December 12

Re: Code runs, but not everything prints.

Posted 01 February 2013 - 06:36 PM

No, you are no longer asking for the marital status and have deleted the variable status that might store this value. Re-instate:

String status;    // you don't need the variables m or s

status = in.next();    // as you had before

if (status.equals("m")) {    // etc.


Your second if statement is still nested within the first one - it should be completely outside, preferably an else if.

Again, if you nest your code properly you will spot these issues more quickly.

This post has been edited by andrewsw: 01 February 2013 - 06:38 PM

Was This Post Helpful? 1
  • +
  • -

#7 Vato116  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-October 12

Re: Code runs, but not everything prints.

Posted 01 February 2013 - 06:58 PM

Thanks. For that it really cleared things up. It seems to be working fine.

I have a question if i wanted to calculate the tax on a number specifically over a certain number but not over another number how should i do that?

For example in:
if (income <=32000)
{
System.out.println("Tax Paid: " +800 * 0.02);
{



how could i write it so that the tax is 0.1 on the amount over 8000 but not over 32,000?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1