Results 1 to 4 of 4
  1. #1
    Shea6892 is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Help with a simple tax program

    So I have to write a program that uses a switch case statement to calculate income taxed and amount owed. The problem I am having is the teacher wants input validation in place for the input of whether you are single, married, or cohabited and also so Grossed income and number of exemptions are greater than or equal to zero. Should I just try to use do loops to do it or? Help is much appreciated thanks.

    Edit: Sorry forgot code.

    Java Code:
    import java.util.Scanner;
    
    public class taxProg {
    	
    		
    		
    		public static void main(String[] args){
    			taxPrep();
    		}
    		
    		public static void taxPrep(){
    			Scanner stdIn = new Scanner(System.in);
    			int statu = 0;
    			double exemp;
    			double taxRate;
    			double taxIncome;
    			double grossIncome;
    			double taxOwed;
    			
    			
    			
    			System.out.println("Are you (s)ingle, (m)arried, or (c)ohabiting?: ");
    			String status = stdIn.next();
    			
    if(status.equals("s")){
    				
    				statu = 0;
    			} else if(status.equals("S")){
    				statu = 0;
    			
    				}else if(status.equals("m")){
    					statu = 1;
    				}else if(status.equals("M")){
    					statu = 1;
    				}else if(status.equals("c")){
    					statu = 2;
    				}else if(status.equals("C")){
    					statu = 2;
    				} else { System.out.println("You didn't enter a valid option. Enter s for single, m for married, or c for cohabiting: ");
    					status = stdIn.next();
    				}
    			
    			System.out.println("Enter gross income: ");
    			grossIncome = stdIn.nextDouble();
    			
    			System.out.println("Enter amount of exemptions: ");
    			exemp = stdIn.nextDouble();
    			exemp *= 1000;
    			
    			
    			
    
    			switch (statu) {
    			case 0: 
    					taxIncome = grossIncome - exemp;
    					taxRate = 0.20;
    					taxOwed = taxIncome * taxRate;
    					
    					System.out.println("T A X   S U M M A R Y");
    					System.out.println("Tax rate: 20.0%");
    					System.out.println("Taxable income: $" + taxIncome);
    					System.out.println("Taxes owed: $" + taxOwed);
    				
    			break;
    			case 1: taxIncome = grossIncome - exemp;
    			taxRate = 0.25;
    			taxOwed = taxIncome * taxRate;
    			
    			System.out.println("T A X   S U M M A R Y");
    			System.out.println("Tax rate: 25.0%");
    			System.out.println("Taxable income: $" + taxIncome);
    			System.out.println("Taxes owed: $" + taxOwed);
    			break;
    			case 2: taxIncome = grossIncome - exemp;
    					if(taxIncome < 20000){
    						taxRate = .10;
    					}else if(taxIncome > 20000 && taxIncome < 50000){
    						taxRate = .15;
    						}else if(taxIncome > 50000){
    							taxRate = .30;
    						
    					taxOwed = taxIncome * taxRate;
    					
    					System.out.println("T A X   S U M M A R Y");
    					System.out.println("Tax rate: 25.0%");
    					System.out.println("Taxable income: $" + taxIncome);
    					System.out.println("Taxes owed: $" + taxOwed);
    			}
    		}
    }
    	
    }

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Help with a simple tax program

    In case you want to vaildate an input, use the do while loop.
    For example, if you want a positive number:

    Java Code:
    do{
    	input = rd.nextInt();
    }while(input<=0);
    Last edited by tnrh1; 02-03-2012 at 03:27 PM.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default Re: Help with a simple tax program

    instead of

    }else if(status.equals("m")){
    statu = 1;
    }else if(status.equals("M")){
    statu = 1;


    you could use one if with String.compareToIgnoreCase()

  4. #4
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Help with a simple tax program

    And just for your information, if you want make 1 change and ask 2 questions you can use the booleans operators just as OR (||).

Similar Threads

  1. Very Simple Program But Can't Get Right
    By mike_ in forum New To Java
    Replies: 3
    Last Post: 11-21-2011, 01:45 AM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  3. simple program
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 05-01-2011, 10:03 PM
  4. Simple program
    By 479320 in forum New To Java
    Replies: 6
    Last Post: 04-08-2011, 08:27 AM
  5. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 12:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •