Results 1 to 2 of 2
Thread: Java code error!~ Please help
- 10-15-2010, 03:00 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
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.
-
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:
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.Java Code:if(m>109250 && m<=166500) // [color="red"]*** no semi-colon here ***[/color] taxes = (m- 166500)*0.305 +24393.75; }Last edited by Fubarable; 10-15-2010 at 03:53 AM.
Similar Threads
-
error in this code
By gradiente99 in forum EclipseReplies: 11Last Post: 07-20-2010, 05:58 PM -
Error Code???
By andmartha in forum New To JavaReplies: 11Last Post: 10-04-2008, 02:16 AM -
trying to improve code to avoid java.lang.OutOfMemory error
By bdyarem in forum New To JavaReplies: 16Last Post: 08-05-2008, 11:34 AM -
Pls help with a code error.
By saytri in forum New To JavaReplies: 8Last Post: 12-24-2007, 08:10 PM -
error in code
By dirtycash in forum New To JavaReplies: 2Last Post: 12-06-2007, 11:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks