Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:
    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);
    }
    }

    Thank you in advance.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

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

    Hello obie!
    Quote Originally Posted by obie View Post
    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;
    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
    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.
    else if (pay >=198700){
    		    pay = pay - 198700;
    		    pay = pay * 0.20 + 100;
    }

    Hope this helps.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    obie (September 6th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    Hello and thanks for your reply!

    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
    But still, I'm very happy that you replied this quick! Gave me a great first time experience with this forum

  5. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

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

    Quote Originally Posted by obie View Post
    Hello and thanks for your reply!

    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
    But still, I'm very happy that you replied this quick! Gave me a great first time experience with this forum
    You are welcome. Glad you figured it out.

Similar Threads

  1. Singletons - I'm having a hard time understand how to implement them.
    By Illanair in forum Object Oriented Programming
    Replies: 6
    Last Post: February 16th, 2012, 05:49 PM
  2. I am having a hard time with my code
    By SandeeBee in forum What's Wrong With My Code?
    Replies: 14
    Last Post: November 12th, 2011, 09:34 AM
  3. Having a hard time figuring out how to make my code work
    By iainnitro in forum Loops & Control Statements
    Replies: 2
    Last Post: September 6th, 2011, 07:48 AM
  4. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 10:12 PM
  5. new to java.... having a hard time trying to read code
    By Newbie_96 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 6th, 2011, 01:51 AM