Results 1 to 12 of 12
Thread: Java Code running problem
- 10-25-2010, 08:50 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
Java Code running problem
This is the code
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.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; } }
thank you
- 10-25-2010, 10:02 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Take a look at your if statements inside your main method. Since there is a ";" after each one, they are effectively doing the following:
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.Java Code:if (marital == 1) { ; } if (marital == 2) { : } System.out.println("Your Federal Tax = $" + single(income));
- 10-26-2010, 01:46 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
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
- 10-26-2010, 01:51 AM #4
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 :)
- 10-26-2010, 01:54 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
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?
- 10-26-2010, 01:58 AM #6
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 :)
- 10-26-2010, 02:06 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
would i include the
inside the if statement? or put it above..Java Code:public static double single(double s)
- 10-26-2010, 02:12 AM #8
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 :)
- 10-26-2010, 02:24 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
so i wouldnt use that public double inside the if statement?
- 10-26-2010, 02:42 AM #10
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
could you give me a hint on how i would pass the method im stuck on this last part and everytihng will run complete
- 10-26-2010, 02:50 AM #11
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
ive completed the error this is what i came up with and it works perfectly
thank you for all your help!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; } }
- 10-26-2010, 04:07 AM #12
Similar Threads
-
Problem running Java with XP Pro SP2
By swarv in forum New To JavaReplies: 16Last Post: 06-23-2010, 01:58 PM -
Problem in running Java swing wizard in jre 1.6 while it is running in jre 1.4
By Sanjay Dwivedi in forum AWT / SwingReplies: 0Last Post: 08-26-2009, 01:03 PM -
Running .java-files won't work/compile does!(Code inside)
By wyldstyle in forum New To JavaReplies: 6Last Post: 02-06-2009, 08:05 PM -
Problem in running java programs
By aravind in forum New To JavaReplies: 0Last Post: 07-14-2008, 11:19 AM -
problem while running java code..
By Jjava in forum New To JavaReplies: 0Last Post: 02-08-2008, 06:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks