Results 1 to 2 of 2
  1. #1
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default Java code error!~ Please help

    Java Code:
    import java.util.Scanner;
    public class IRSTaxes 
    {
    
    	/**
    	 * @param args
    	 * @param taxes 
    	 */
    	static double taxes;
    	public static void main(String[] args) 
    	{
    		
    		// TODO Auto-generated method stub
    		System.out.println("Enter marital status (1=single, 2=married) ");
    		Scanner scanner= new Scanner(System.in);
    		int marital = scanner.nextInt();
    		System.out.println("Enter taxable income: ");
    		Scanner scanner1= new Scanner(System.in);
    		double income=scanner.nextDouble();		
    		if (marital == 1)
    		System.out.println("Your Federal Tax = $ " + single(income));
    	}
    
    		
    		public static double single(double s)
    		{
    			
    			if(s > 0 && s <= 27050);
    			taxes = (s-27050)*0.15;
    			
    			if(s > 27050 && s <= 65550);
    			taxes= (s -65550)*0.275 +4057.50;
    			
    			if(s > 65550 && s <= 136750);
    			taxes= (s -136750)*0.305 +14645.00;
    			
    			if (s > 136750 && s <= 297350);
    			taxes= (s -297350)*0.355 +36361.00;
    			
    			if (s > 297350);
    			taxes= (s -297350)*0.391 + 93374.00;
    			return taxes;
    		}
    		
    		
    			
    	
    
    	public static double married(double m)
    	{
    		if(m>0 && m <=45200);
    		taxes = (m-45200)*0.15;
    		
    		if(m>45200 && m<= 109250);
    		taxes = (m- 109250)*0.275 +6780.00;
    		
    		if(m>109250 && m<=166500);
    		taxes = (m- 166500)*0.305 +24393.75;
    		
    		if(m>166500 && m<=297350);
    		taxes = (m- 297350)*0.355 +41855.00;
    		
    		if(m>297350);
    		taxes = (m- 297350)*0.391 +88306.00;
    	
    	return taxes;
    	}
    
    }

    when i run the program i get what i want until the federal tax printing. It comes out with a negative number and i cant find out why.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by socboy6579 View Post
    when i run the program i get what i want until the federal tax printing. It comes out with a negative number and i cant find out why.
    Double check your formulas as they look wrong. They look as if you'll always subtract a number bigger than the income.

    Also, your if statements are wrong. Something like this:
    Java Code:
       if(m>109250 && m<=166500);
       taxes = (m- 166500)*0.305 +24393.75;

    will be interpreted by the compiler as this:
    Java Code:
       if(m>109250 && m<=166500)
          ; // this empty statement will be run or not depending on the boolean test in the if above
       taxes = (m- 166500)*0.305 +24393.75; // this line will always be run

    Instead, get rid of the semi-colon at the end of the if statement and always enclose any block or loop such as your if blocks, in curly braces:
    Java Code:
       if(m>109250 && m<=166500)  // [color="red"]*** no semi-colon here ***[/color]
          taxes = (m- 166500)*0.305 +24393.75; 
       }
    But again, this formula looks wrong since m is always going to be less than or equal to 166500, and if you subtract 1665500 from m, it will always be less than or equal to 0.
    Last edited by Fubarable; 10-15-2010 at 03:53 AM.

Similar Threads

  1. error in this code
    By gradiente99 in forum Eclipse
    Replies: 11
    Last Post: 07-20-2010, 05:58 PM
  2. Error Code???
    By andmartha in forum New To Java
    Replies: 11
    Last Post: 10-04-2008, 02:16 AM
  3. Replies: 16
    Last Post: 08-05-2008, 11:34 AM
  4. Pls help with a code error.
    By saytri in forum New To Java
    Replies: 8
    Last Post: 12-24-2007, 08:10 PM
  5. error in code
    By dirtycash in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 11:40 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
  •