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 12 of 12

Thread: Help me with the confirm dialog and counter

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me with the confirm dialog and counter

    Hello people, I searched on the web, searched on this site as well but I couldn't get help. So please help me out. This is the code I have written so far. And yes this is an assignment.
    This program calculates tax from multiple tax payers.
    import javax.swing.JOptionPane;
    public class CalcutateTax
    {
    	public static void main (String [] args)
    	{
     
    				String firstName;
    				double grossIncome;
    				double netIncome;
    				int children;
    				double dependencyExemption;
    				double taxDue;
    				firstName = JOptionPane.showInputDialog (null, "What is your first name? ");
    				grossIncome = Double.parseDouble(JOptionPane.showInputDialog("What is your gross income? "));
    				children = Integer.parseInt(JOptionPane.showInputDialog(" And " + firstName + " how many children do you have? "));
    				dependencyExemption = 3000*children;
    				netIncome = grossIncome - dependencyExemption;
    				taxDue = calculateTax (netIncome);
    				JOptionPane.showMessageDialog ( null, "Name:" + firstName +"\nGross income: $ " + grossIncome + "\nNumber of children: "+ children + "\nTax due: $" + taxDue);
    				int n = JOptionPane.showConfirmDialog( null,"Calculate another tax returns?", "Confirmation", JOptionPane.YES_NO_OPTION);
     
     
    }
     
    	public static double calculateTax(double theNetIncome)
    	{
    		double theTaxDue;
    		if (theNetIncome > 50000)
    		{
    			theTaxDue = (15*50000)/100 + (25 *(theNetIncome-50000)/100) ;
    		}
    		else if (theNetIncome>0)
    		{
    			theTaxDue = (15*theNetIncome)/100;
    		}
    		else
    		{
    			theTaxDue = 0;
    		}
    		return theTaxDue;
     
    }
    }
    -----------------------------------------------------------------------------------------------------------------
    The problem is I cant think how to ask the use if he wants to calculate tax due for another taxpayer. If the user says yes, keep calculating, otherwise exit from the program. And how do I keep count of how many people got their tax calculated? Say for example,
    JOptionPane.showMessageDialog (null, " We calculated tax for " + xnumber + " number of people.");

    This is the question asked on my assignment
    ask the user if he wants to calculate the tax due for another taxpayer
    if so, do it again
    At the end of the main method, output a message in a dialog box that says: Hello, We calculated taxes for [number of taxpayers].
    replace [number of taxpayers] with the actual number of taxpyers you calculated taxes for.
    Last edited by howarddermaus1111; March 29th, 2014 at 05:25 PM. Reason: wrong format


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help me with the confirm dialog and counter

    Sounds like you need a loop.
    After asking the user the question,
    either continue with another iteration of the loop
    or exit the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with the confirm dialog and counter

    Quote Originally Posted by Norm View Post
    Sounds like you need a loop.
    After asking the user the question,
    either continue with another iteration of the loop
    or exit the loop.
    This is where i am confused. I tried using the do while loop but since showconfirm dialog is an int what do I compare it with? and how do I palce my counter?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help me with the confirm dialog and counter

    The value returned by the showconfirm dialog is defined in the API doc for that method. There are several static variables that define the possible returned values. Use one of them to compare against what the method returned.
    For example: JoptionPane.CANCEL_OPTION is the value returned when the user cancels.

    The counter should be defined and initialized outside the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with the confirm dialog and counter

    Quote Originally Posted by Norm View Post
    The value returned by the showconfirm dialog is defined in the API doc for that method. There are several static variables that define the possible returned values. Use one of them to compare against what the method returned.
    For example: JoptionPane.CANCEL_OPTION is the value returned when the user cancels.

    The counter should be defined and initialized outside the loop.
    Please don't get offended. I really didn't get what you said. I am new to this whole java thing. Care to explain a little more?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help me with the confirm dialog and counter

    Which part are you confused about?
    the loop
    the counter
    the return value
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with the confirm dialog and counter

    I am confused on all three part. I know how to do a loop but in this case I cannot decide how to and from where to begin with. I believe if I can do the loop I should be able to take care of the counter, and the return value.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help me with the confirm dialog and counter

    The counter is just an int that is set to 0 outside the loop and incremented by one inside the loop for each event you want to count.

    The return value should be saved in an int variable and then tested:
    int retVal = JOptionPane.show....(...); // ask question and get response
    then compare the contents of retVal against the static fields defined in the JOptionPane class as needed.
    One of the values is JOptionPane.CANCEL_OPTION for when the user pressed Cancel.
    if(revVal == JOptionPane.CANCEL_OPTION) {
    // process user cancelled it
    }
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with the confirm dialog and counter

    Quote Originally Posted by Norm View Post
    The counter is just an int that is set to 0 outside the loop and incremented by one inside the loop for each event you want to count.

    The return value should be saved in an int variable and then tested:
    int retVal = JOptionPane.show....(...); // ask question and get response
    then compare the contents of retVal against the static fields defined in the JOptionPane class as needed.
    One of the values is JOptionPane.CANCEL_OPTION for when the user pressed Cancel.
    if(revVal == JOptionPane.CANCEL_OPTION) {
    // process user cancelled it
    }

    import javax.swing.JOptionPane;
    public class AssignmentSix0
    {
    	public static void main (String [] args)
    	{
    		int num1 = 0;
    		int maxNum = 100;
    		while (num1 < maxNum)
    		{
    			int answer = JOptionPane.showConfirmDialog(null, "Calculate tax again?");
    			if (answer == JOptionPane.CANCEL_OPTION)
    			{
    				System.exit(0);
    			}
    		if (answer == JOptionPane.YES_OPTION)
    		{
    			num1++;
    		}
    			JOptionPane.showMessageDialog(null, "We calculated tax for " + num1 + " people .");
    }
    }
    }

    I think I did like you said. Correct me if I am wrong. It works if I declare int maxNum but without declaring it, it doesn't work

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help me with the confirm dialog and counter

    The while could use a boolean of true if you want the loop to continue untl the user says to stop.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with the confirm dialog and counter

    would you mind showing what you mean?
    import javax.swing.JOptionPane;
    public class AssignmentSix0
    {
    	public static void main (String [] args)
    	{
    		boolean calculateAgain = true;
    		int answer ;
    		while (calculateAgain)
    		{
    			 answer = JOptionPane.showConfirmDialog(null, "Calculate tax again?", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION);
    			calculateAgain= (answer == JOptionPane.YES_OPTION);
    		}//end while
     
     
    }//main
    }//class


    Did you mean this way? Where do I put the count if this is correct?
    Last edited by howarddermaus1111; March 29th, 2014 at 09:34 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help me with the confirm dialog and counter

    That looks like a good while loop.

    What is count supposed to do? Is it the same as num1 in post#9?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. open dialog
    By javabegin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 27th, 2014, 07:26 AM
  2. Java Dialog Box HELP
    By Green Light in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 1st, 2013, 02:33 AM
  3. Take dialog out of browser window
    By IliaPrikhodko in forum Web Frameworks
    Replies: 0
    Last Post: April 15th, 2012, 07:59 PM
  4. [SOLVED] Dialog Woes...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 25th, 2012, 09:08 PM
  5. [SOLVED] Help with dialog box
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 5
    Last Post: August 4th, 2009, 11:06 AM