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

    Default Java Code running problem

    This is the code
    Java Code:
    import java.util.Scanner;
    public class Taxes 
    	{
    		static double taxes;
    		public static void main(String[] args) 
    		{
    		// Prompting 
    		System.out.println("Enter maritial 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);
    		if(marital ==2);
    		System.out.println("Your Federal Tax = $" + single(income));
    		}		
    		
    		
    		public static double single(double s)
    		{
    			//finding out tax for single
    			
    			if (s > 0 && s<=27050)
    			taxes= .15*(s-0);
    			
    			if (s > 27050 && s<=65550)
    			taxes= 4057.50 + 0.275*(s-27050);
    			
    			if (s > 65550 && s<=136750)
    			taxes= 14645.00 + 0.305*(s-65550);
    			
    			if (s > 136750 && s<=297350)
    			taxes= 36361.00 + .355*(s-136750);
    			
    			if (s > 297350)
    			taxes= 93374.00 + .391*(s-297350);
    			return taxes;
    		}
    		
    		public static double married(double m)
    		{
    			
    		//finding out tax for double
    		if (m > 0 && m <= 45200)
    		taxes= .15*(m-0);
    		
    		if (m > 45200 && m <= 109250)
    		taxes= 6780.00 + .275*(m-45200);
    		
    		if (m > 109250 && m <= 166500)
    		taxes= 24393.75 + .305*(m-109250);
    		
    		if (m > 166500 && m <= 297350)
    		taxes= 41855.00 + .355*(m-166500);
    		
    		if (m> 297350)
    		taxes= 88306.00 + .391*(m-297350);
    		return taxes;
    		
    		}
    		
    
    	}
    when i run this code, i need to run it as for single and married when it is prompted. even though i have the married if statements, when input for married it still refers to the top formulas. when i refer to 1 for single the formulas work fine and output is correct, the output for both the single and the married are the same even though the married is supposed to refer to the below portion of if statements and not the top. how can i change this i am stuck here and my code will run perfectly.

    thank you

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Take a look at your if statements inside your main method. Since there is a ";" after each one, they are effectively doing the following:

    Java Code:
    if (marital == 1) {
         ;
    }
    if (marital == 2) {
         :
    }
    System.out.println("Your Federal Tax = $" + single(income));
    I recommend always using brackets to prevent this type of confusion as I've come across it quite a bit, even in production code, which leads to all sorts of issues.

  3. #3
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    what im saying is that... im trying to get when i input 2 for the scanner it will do the tasks for the married if statements.. when i do either one its the same thing as the formulas are clearly diffrent with differnt numbers which will give me diff rent number if ran correctly. but when i run the same salary for each married and single its the same, even though married is diffrent from single. im trying to figure out why it wont run correctly if i input 2 meaning married. if i fix this issue my code will work perfectly

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Your if statements are not doing anything the way they are now. There is no code inside of them. And then after these statements you are calling only the single() method every time.

    Java Code:
    if(marital ==1);
    if(marital ==2);
    System.out.println("Your Federal Tax = $" + single(income));
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    i have the if statements for both married and single correct.. could you tell me how i would use them properly the if statements would i put the if statements for the single under the marital==1?

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Yes, exactly. Except don't use a semi-colon ";" after the if statement. You will use brackets and then put your code in between those brackets underneath. Example:

    Java Code:
    if(marital == 1)
    {
         // Your single code here
    }
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    would i include the
    Java Code:
    public static double single(double s)
    inside the if statement? or put it above..

  8. #8
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    All you have to do is call the method from inside the if statement. Not put the entire method inside of the if statement. Call the single() method just like you did in your output statement, but inside the if statement.
    Sincerely, Joshua Green
    Please REP if I help :)

  9. #9
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    so i wouldnt use that public double inside the if statement?

  10. #10
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    could you give me a hint on how i would pass the method im stuck on this last part and everytihng will run complete

  11. #11
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    ive completed the error this is what i came up with and it works perfectly

    Java Code:
    import java.util.Scanner;
    public class Taxes 
    	{
    		private static final double income = 0;
    		static double taxes;
    		public static void main(String[] args) 
    		{
    		// Prompting 
    		System.out.println("Enter maritial 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));
    		}
    		if (marital==2){
    			System.out.println("Your Federal Tax = $" + married(income));
    		}
    		}
    		
    			public static double single(double s)
    			{
    				//finding out tax for single
    				
    				if (s > 0 && s<=27050)
    				taxes= .15*(s-0);
    				
    				if (s > 27050 && s<=65550)
    				taxes= 4057.50 + 0.275*(s-27050);
    				
    				if (s > 65550 && s<=136750)
    				taxes= 14645.00 + 0.305*(s-65550);
    				
    				if (s > 136750 && s<=297350)
    				taxes= 36361.00 + .355*(s-136750);
    				
    				if (s > 297350)
    				taxes= 93374.00 + .391*(s-297350);
    				return taxes;
    		}
    			
    	public static double married(double m)
    		{
    			
    		//finding out tax for double
    		if (m > 0 && m <= 45200)
    		taxes= .15*(m-0);
    		
    		if (m > 45200 && m <= 109250)
    		taxes= 6780.00 + .275*(m-45200);
    		
    		if (m > 109250 && m <= 166500)
    		taxes= 24393.75 + .305*(m-109250);
    		
    		if (m > 166500 && m <= 297350)
    		taxes= 41855.00 + .355*(m-166500);
    		
    		if (m> 297350)
    		taxes= 88306.00 + .391*(m-297350);
    		return taxes;
    		
    		}
    		
    
    	}
    thank you for all your help!

  12. #12
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    You're welcome! That's exactly how I wanted you to do it too, nice job, and congrats.
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. Problem running Java with XP Pro SP2
    By swarv in forum New To Java
    Replies: 16
    Last Post: 06-23-2010, 01:58 PM
  2. Replies: 0
    Last Post: 08-26-2009, 01:03 PM
  3. Replies: 6
    Last Post: 02-06-2009, 08:05 PM
  4. Problem in running java programs
    By aravind in forum New To Java
    Replies: 0
    Last Post: 07-14-2008, 11:19 AM
  5. problem while running java code..
    By Jjava in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 06:33 AM

Posting Permissions

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