0

I am writing this program that calculates total tax for different tax brackets and when sentinel data is input, I have to add up all the taxes. My question is how can I add all the taxes that were output by my program. Here is the code:

import java.util.Scanner;
public class TaxIncome {

 public static void main(String[] args) {

     Scanner kbd = new Scanner (System.in);

            final double fedtax1 = 0.10;                                                // Tax rates    
             final double fedtax2 = 0.15;                                                   
             final double fedtax3 = 0.33;       

             final double taxbracket1 = 15000;                                          // Tax Brackets         
             final double taxbracket2 = 60000;

             final double maxtax1 = 1500;                                               // Max tax on brackets
            final double maxtax2 = 8250;



     while(true){

         System.out.println("Please input the income, type a negative number to end");
         double income = kbd.nextDouble();
         double totaltax = 0;
         double sumOftotaltax = 0;

         if(income <= taxbracket1)
              totaltax = income * fedtax1;

         else if (income <= taxbracket2 && income > taxbracket1)
             totaltax = ((income-taxbracket1)* fedtax2)+ maxtax1;  

         else if (income > taxbracket2)
             totaltax = ((income-taxbracket2)*fedtax3)+maxtax2;

                     System.out.println("The total tax is $" +totaltax);

        if  (income < 0) break;

        sumOftotaltax = totaltax; 


        System.out.println("The total of the taxes is $" +sumOftotaltax);

          }



 }
 }
Samane
  • 500
  • 7
  • 23
user5400828
  • 71
  • 1
  • 8
  • Move `double sumOftotaltax = 0;` outside WHILE loop, before start of it. Use `sumOftotaltax = sumOftotaltax + totaltax;` instead of `sumOftotaltax = totaltax; `. Move `System.out.println("The total of the taxes is $" +sumOftotaltax);` outside WHILE loop, after it. – hagrawal7777 Oct 03 '15 at 18:31

1 Answers1

0

Create a new sum variable sumOf_sumOftotaltax which would add all the sums out.

sumOf_sumOftotaltax += sumOftotaltax;

Or create an ArrayList to hold each individual one.

List <String>totalTaxes = new ArrayList<String>();

totalTaxes.add(sumOftotaltax);

However if you are looking to provide sums across executions of the program you will have to save them in a file or database.

ergonaut
  • 6,929
  • 1
  • 17
  • 47