# Having a hard time to solve this (probably easy for the rest of you)

• September 6th, 2012, 09:06 AM
obie
Having a hard time to solve this (probably easy for the rest of you)
Hi there! Im new to Java and I'm also new to these forums. And hopefully i can get some advice to point me in the right direction.

My assignment was to make a program that calculates and writes a persons taxes, based on their income.
If the income were less then (or the same) as 8900. 0 would be payed in taxes.
If the income were more than 8900 but less than 198700. 100 would be payed in taxes.
If the income were more than (or the same) as 198700. 100 + 20% of the income above 198700 would be payed in taxes.

And also, before the program shuts down it should ask me if i want to calculate some more.

So i have it almost done. It's just something that is missing, and i just can't figure it out. Maybe u guys can help me out a bit. Here's the code i've written so far:
Code Java:

```import javax.swing.*; public class Test { public static void main (String[] arg) {   int knappNr; do{ String tax = JOptionPane.showInputDialog ("Write your income"); double pay = Double.parseDouble (pay);   if (pay <=8900) pay = 0; else if (pay >=198700) pay = pay - 198700; pay = pay * 0.20 + 100; else pay = 100; JOptionPane.showMessageDialog (null, "Your tax to pay is " + pay+" dollar"); knappNr = JOptionPane.showConfirmDialog (null, "Do you want to make another calculate?", "Question", JOptionPane.YES_NO_OPTION); } while (knappNr == 0); } }```

• September 6th, 2012, 09:42 AM
andreas90
Re: Having a hard time to solve this (probably easy for the rest of you)
Hello obie!
Quote:

Originally Posted by obie
So i have it almost done. It's just something that is missing, and i just can't figure it out. Maybe u guys can help me out a bit.

For your next posts it would help if you posted what the problem is (error, exception, unexpected behaviour).

But I can see two problems with your code;
Code :

`double pay = Double.parseDouble (pay);`
The Double.parseDouble method takes a String parameter but you pass it a double. You need to pass it what is entered as income by the user.
In addition, suppose it took a double parameter you cannot pass a parameter which hasn't been initialized to a method.

And also
Code :

```else if (pay >=198700) pay = pay - 198700; pay = pay * 0.20 + 100;```
That way when the condition is true only the first statement will be executed. If you want them both to execute then you should put them in the same block i.e. in the same pair of {}'s.
Code :

```else if (pay >=198700){ pay = pay - 198700; pay = pay * 0.20 + 100; }```

Hope this helps.
• September 6th, 2012, 09:49 AM
obie
Re: Having a hard time to solve this (probably easy for the rest of you)

Actually it worked with just putting the {} in the code. And the best part is a manage to solve it before i read your comment :D
But still, I'm very happy that you replied this quick! Gave me a great first time experience with this forum :)
• September 6th, 2012, 09:54 AM
andreas90
Re: Having a hard time to solve this (probably easy for the rest of you)
Quote:

Originally Posted by obie