Ok so I'm in need of some help.In the first part of this code, I need both the 'output' and 'enter' to be returned back to the calling method but one is a String and one is an int. When I compile and run the program, I get a null in the Filing Status column and $0.00 in the Tax Amount column. Can someone please help. I have no idea what to do. Below is my code.

import java.util.Scanner;			// Needed for the Scanner class
import java.text.NumberFormat;		// Needed for Number Format class

/**
 This program finds a person's federal income tax.
 CPS 180
 Abhik Roy
 Homework 6
 Friday, March 2, 2009
 */

public class Income 
	
	{
		
		// Main method.
		public static void main(String[ ] args)
		
		{
			double taxAmount;
			boolean validInput = true;
			boolean validEnter = true;
			int enter = 0;
			double tax1 = 0.0;
			double tax2 = 0.0;
			double tax3 = 0.0;
			double tax4 = 0.0;
			double tax5 = 0.0;
			double tax6 = 0.0;
			double taxfinal1 = 0.0;
			double taxfinal2 = 0.0;
			double taxfinal = 0.0;
			double alphatax = 0.0;
			double betatax = 0.0;
			String output = null;
			String menu = null;
			
			Scanner keyboard = new Scanner (System.in);
			
			System.out.print("\nEnter your taxable income amount: $");
			
			do
			{
				taxAmount = keyboard.nextInt( );			// Stores the chosen movie.
				validInput = true;
				if ( (taxAmount < 50000) )					// Defines the invalid boundaries (but true by definition).
				{
					System.out.print ("\n(You must an amount greater than or equal to $50,000): $");
					validInput = false;										 
				}
			}while(!validInput);
			
			// A method call to displayMenu
			displayMenu (menu, validEnter, enter, output);
			
			// A method to call to computeIncometax
			computeIncometax (taxAmount, enter, tax1, tax2, tax3, tax4, tax5, tax6, taxfinal1, taxfinal2, taxfinal, alphatax, betatax);
			
			// A method to call to displayFinal
			displayFinal (taxAmount, output, taxfinal);
			
			
			
			System.exit(0);
		}
		
		public static String displayMenu(String menu, boolean validEnter, int enter, String output)
		{
			Scanner keyboard = new Scanner (System.in);
			
			System.out.print ("\n ");
			System.out.print ("**************************************************************");
			System.out.print ("\n *			Filing Status Menu		      *");
			System.out.print ("\n *		1 - Single				      *");
			System.out.print ("\n *		2 - Married Filing Jointly		      *");
			System.out.print ("\n *		3 - Marries Filing Separately		      *");
			System.out.print ("\n *		4 - Head of Household			      *");
			System.out.print ("\n **************************************************************");
			System.out.print ("\n ");
			
			System.out.print("\nPlease choose a filing status number: ");
			
			do
			{
				enter = keyboard.nextInt( );			// Stores the chosen status number.
				validEnter = true;
				if ( (enter < 1) || (enter > 4))		// Defines the invalid boundaries (but true by definition).
				{
					System.out.print ("\n(You must pick a status number 1 through 4): ");
					validEnter = false;										 
				}			
				
			}while(!validEnter);	
			
			if (enter == 1)
				output = "Single";
			else if (enter == 2)
				output = "Married Filing Jointly";
			else if (enter == 3)
				output = "Married Filing Separately";
			else if (enter == 4)
				output = "Head of Household";
			else;
		
			return output;
			
		}
		
		public static double  computeIncometax(double taxAmount, int enter, double tax1, double tax2, double tax3, double tax4, double tax5, double tax6, double taxfinal1, double taxfinal2, double taxfinal, double alphatax, double betatax)
		{
			tax1 = 11158.50 + 0.31*(taxAmount - 49300.00);
			tax2 = 5100.00 + 0.28*(taxAmount - 34000.00);
			tax3 = 18582.00 + 0.31*(taxAmount - 82150.00);
			tax4 = 9291.00 + 0.31*(taxAmount - 41075.00);
			tax5 = 4095.00 + 0.28*(taxAmount - 27300);
			tax6 = 16177.00 + 0.31*(taxAmount - 70450);
			
			
			
			if (taxAmount > 34000 && taxAmount < 82150)
				taxfinal1 = tax2;
			else 
				taxfinal1 = tax3;
			
			if (taxAmount > 27300 && taxAmount < 70450)
				taxfinal2 = tax5;
			else
				taxfinal2 = tax6;
			
			if (enter == 1)
				taxfinal1 = tax1;
			else if (enter == 2)
				alphatax = taxfinal1;
			else if (enter == 3)
				taxfinal2 = tax4;
			else if (enter == 4)
				betatax= taxfinal1;
			else;
			
			if (enter == 1)
				taxfinal = taxfinal1;
			else if (enter == 2)
				taxfinal = taxfinal1;
			else if (enter == 3)
				taxfinal = taxfinal2;
			else if (enter == 4)
				taxfinal = taxfinal2;
			else;
			
			return taxfinal;
				
		}
		
	public static void displayFinal(double taxAmount, String output, double taxfinal)
		{
			
			NumberFormat nf = NumberFormat.getNumberInstance( );	// Defines the number format.
			nf.setMinimumFractionDigits(2);							// Sets the minimum decimal places.
			nf.setMaximumFractionDigits(2);							// Sets the maximum decimal places.
			
			// Display the final statement
			System.out.println(" ");
			
			System.out.println("   Taxable Income Amount" + "	     Filing Status" + "	         Tax Amount" );
			System.out.println("-------------------------------------------------------------------------------");
			System.out.println("        $" + nf.format(taxAmount) + "               " +  output + "       $" + nf.format(taxfinal));
			
			
			System.out.println(" ");
		}
	}

Recommended Answers

All 13 Replies

I need both the 'output' and 'enter' to be returned back to the calling method but one is a String and one is an int

You have many options here one is create a new class which has those two as its member properties and return a reference to this object from that method.
Second is put them both inside a "String" array of size "2" and return a reference to that array from the method and in the calling function extract the values from the array. For ex in the method you could have :-

String[] arrayOfValues = { output, Integer.toString(enter)};

And in the caller method you could extract the values from the "String" array as :-

output = arrayOfValues[0];
enter = Integer.parseInt(arrayOfValues[1]);

Next you could also return a Properties (or some other classes part of the Collections Framework ) object with the appropriate name value pairs mapped in it.

commented: Very helpful +1

Think about separating the logic and the UI fully. Java's an OO language, so use objects.
Define a Tax object, with the taxable amount, tax bands etc as its instance variables, and the tax calculation as a public method. Include public "set" methods for the taxable amount and filing status - these should do the validation and either throw an exception or return an error message String if the input is bad.
The UI then creates a new Tax object, displays prompts, gets the input, and calls the "set" methods, displaying any errors that get returned. Finally the UI can call the tax calc method and display its result.

Yes,use object man............thatz the way to do it

I don't know how to do OO programming yet. I'm taking an introductory level Java course and we're not there yet. I tried to do what stephen84s wrote, but I think I am doing it incorrectly. Can someone else or stephen84s give me some more insight based on my code. Again, I am very new to Java. Thanks.

Show us what you tried ?

Thanks for your response. I have tried a bunch of things...but ultimately I always come back to this. My logic must be off.

import java.util.Scanner;			// Needed for the Scanner class
import java.text.NumberFormat;		// Needed for number format class

/**
 This program finds a person's federal income tax.
 CPS 180
 Abhik Roy
 Homework 6
 Friday, March 2, 2009
 */

public class Income 
	
	{
		
		// Main method.
		public static void main(String[ ] args)
		
		{
			double taxAmount;
			boolean validInput = true;
			boolean validEnter = true;
			int enter = 0;
			double tax1 = 0.0;
			double tax2 = 0.0;
			double tax3 = 0.0;
			double tax4 = 0.0;
			double tax5 = 0.0;
			double tax6 = 0.0;
			double taxfinal1 = 0.0;
			double taxfinal2 = 0.0;
			double taxfinal = 0.0;
			double alphatax = 0.0;
			double betatax = 0.0;
			String output = null;
			String menu = null;
			String[] arrayOfValues = { output, Integer.toString(enter)};

			output = arrayOfValues[0];
			enter = Integer.parseInt(arrayOfValues[1]);
			
			Scanner keyboard = new Scanner (System.in);
			
			System.out.print("\nEnter your taxable income amount: $");
			
			do
			{
				taxAmount = keyboard.nextInt( );			// Stores the chosen movie.
				validInput = true;
				if ( (taxAmount < 50000) )					// Defines the invalid boundaries (but true by definition).
				{
					System.out.print ("\n(You must an amount greater than or equal to $50,000): $");
					validInput = false;										 
				}
			}while(!validInput);
			
			// A method call to displayMenu
			displayMenu (menu, validEnter, enter, output);
			
			// A method to call to computeIncometax
			computeIncometax (taxAmount, enter, tax1, tax2, tax3, tax4, tax5, tax6, taxfinal1, taxfinal2, taxfinal, alphatax, betatax);
			
			// A method to call to displayFinal
			displayFinal (taxAmount, output, taxfinal);
			
			
			
			System.exit(0);
		}
		
		public static int displayMenu(String menu, boolean validEnter, int enter, String output)
		{
			Scanner keyboard = new Scanner (System.in);
			
			System.out.print ("\n ");
			System.out.print ("**************************************************************");
			System.out.print ("\n *			Filing Status Menu		      *");
			System.out.print ("\n *		1 - Single				      *");
			System.out.print ("\n *		2 - Married Filing Jointly		      *");
			System.out.print ("\n *		3 - Marries Filing Separately		      *");
			System.out.print ("\n *		4 - Head of Household			      *");
			System.out.print ("\n **************************************************************");
			System.out.print ("\n ");
			
			System.out.print("\nPlease choose a filing status number: ");
			
			do
			{
				enter = keyboard.nextInt( );			// Stores the chosen status number.
				validEnter = true;
				if ( (enter < 1) || (enter > 4))		// Defines the invalid boundaries (but true by definition).
				{
					System.out.print ("\n(You must pick a status number 1 through 4): ");
					validEnter = false;										 
				}				
			}while(!validEnter);	
			
			
			if (enter == 1)
				output = "Single";
			else if (enter == 2)
				output = "Married Filing Jointly";
			else if (enter == 3)
				output = "Married Filing Separately";
			else if (enter == 4)
				output = "Head of Household";
			else;
		
			
			String myString = Integer.toString(enter);
			
			return enter;
			return output;
			
			
			
		}
		
		public static int  computeIncometax(double taxAmount, int enter, double tax1, double tax2, double tax3, double tax4, double tax5, double tax6, double taxfinal1, double taxfinal2, double taxfinal, double alphatax, double betatax)
		{
			tax1 = 11158.50 + 0.31*(taxAmount - 49300.00);
			tax2 = 5100.00 + 0.28*(taxAmount - 34000.00);
			tax3 = 18582.00 + 0.31*(taxAmount - 82150.00);
			tax4 = 9291.00 + 0.31*(taxAmount - 41075.00);
			tax5 = 4095.00 + 0.28*(taxAmount - 27300);
			tax6 = 16177.00 + 0.31*(taxAmount - 70450);
			
			
			
			if (taxAmount > 34000 && taxAmount < 82150)
				taxfinal1 = tax2;
			else 
				taxfinal1 = tax3;
			
			if (taxAmount > 27300 && taxAmount < 70450)
				taxfinal2 = tax5;
			else
				taxfinal2 = tax6;
			
			if (enter == 1)
				taxfinal1 = tax1;
			else if (enter == 2)
				alphatax = taxfinal1;
			else if (enter == 3)
				taxfinal2 = tax4;
			else if (enter == 4)
				betatax= taxfinal1;
			else;
			
			if (enter == 1)
				taxfinal = taxfinal1;
			else if (enter == 2)
				taxfinal = taxfinal1;
			else if (enter == 3)
				taxfinal = taxfinal2;
			else if (enter == 4)
				taxfinal = taxfinal2;
			else;
			
			return enter;
				
		}
		
	public static void displayFinal(double taxAmount, String output, double taxfinal)
		{
			
			NumberFormat nf = NumberFormat.getNumberInstance( );	// Defines the number format.
			nf.setMinimumFractionDigits(2);							// Sets the minimum decimal places.
			nf.setMaximumFractionDigits(2);							// Sets the maximum decimal places.
			
			// Display the final statement
			System.out.println(" ");
			
			System.out.println("   Taxable Income Amount" + "	     Filing Status" + "	         Tax Amount" );
			System.out.println("-------------------------------------------------------------------------------");
			System.out.println("        $" + nf.format(taxAmount) + "               " +  output + "       $" + nf.format(taxfinal));
			
			
			System.out.println(" ");
		}
	}
String myString = Integer.toString(enter);
			
return enter;
return output;

You completely missed the point I was trying to say. I had told you to package both those values into a String array and return a reference to that array back to the calling method, like here :-

String[] arrayOfValues = { output, Integer.toString(enter)};
return arrayOfValues;

Also you will have to modify the return type of your method to accommodate that.
BTW didn't the compiler complain about unreachable code ???

Ok so I think I've got everything working ok and it compiles w/o errors. Now I'm having trouble getting a value for the filing status and tax amount. For example, when I input 70300, the output is the following:

Taxable Income Amount Filing Status Tax Amount
-------------------------------------------------------------------------------
$70,300.00 null $0.00

below is a copy of my code:

import java.util.Scanner;			// Needed for the Scanner class
import java.text.NumberFormat;		// Needed for number format class

/**
 This program finds a person's federal income tax.
 */

public class Income 
	
	{
		
		// Main method.
		public static void main(String[ ] args)
		
		{
			
			double taxAmount;
			boolean validInput = true;
			boolean validEnter = true;
			double tax1 = 0.0;
			double tax2 = 0.0;
			double tax3 = 0.0;
			double tax4 = 0.0;
			double tax5 = 0.0;
			double tax6 = 0.0;
			double taxfinal1 = 0.0;
			double taxfinal2 = 0.0;
			double taxfinal = 0.0;
			double alphatax = 0.0;
			double betatax = 0.0;
			String output = null;
			double enter = 0;
			String menu = null;
			String[ ] arrayOfValues = { output, Double.toString(enter)};
			double[ ] arrayofNumbers= { taxfinal, enter};
			
			
			Scanner keyboard = new Scanner (System.in);
			
			System.out.print("\nEnter your taxable income amount: $");
			
			do
			{
				taxAmount = keyboard.nextInt( );			// Stores the chosen movie.
				validInput = true;
				if ( (taxAmount < 50000) )					// Defines the invalid boundaries (but true by definition).
				{
					System.out.print ("\n(You must an amount greater than or equal to $50,000): $");
					validInput = false;										 
				}
			}while(!validInput);
			
			// A method call to displayMenu
			displayMenu (arrayOfValues, menu, validEnter, enter= Double.parseDouble(arrayOfValues[1]), output=arrayOfValues[0]);
			
			// A method to call to computeIncometax
			computeIncometax (arrayofNumbers, taxAmount, enter, tax1, tax2, tax3, tax4, tax5, tax6, taxfinal1, taxfinal2, taxfinal, alphatax, betatax);
			
			// A method to call to displayFinal
			displayFinal (taxAmount, output, taxfinal);
			
			
			
			System.exit(0);
		}
		
		public static String[ ] displayMenu(String[ ] arrayOfValues, String menu, boolean validEnter, double enter, String output)
		{
			Scanner keyboard = new Scanner (System.in);
			
			System.out.print ("\n ");
			System.out.print ("**************************************************************");
			System.out.print ("\n *			Filing Status Menu		      *");
			System.out.print ("\n *		1 - Single				      *");
			System.out.print ("\n *		2 - Married Filing Jointly		      *");
			System.out.print ("\n *		3 - Marries Filing Separately		      *");
			System.out.print ("\n *		4 - Head of Household			      *");
			System.out.print ("\n **************************************************************");
			System.out.print ("\n ");
			
			System.out.print("\nPlease choose a filing status number: ");
			
			do
			{
				enter = keyboard.nextInt( );			// Stores the chosen status number.
				validEnter = true;
				if ( (enter < 1) || (enter > 4))		// Defines the invalid boundaries (but true by definition).
				{
					System.out.print ("\n(You must pick a status number 1 through 4): ");
					validEnter = false;										 
				}				
			}while(!validEnter);	
			
			
			if (enter == 1)
				output = "Single";
			else if (enter == 2)
				output = "Married Filing Jointly";
			else if (enter == 3)
				output = "Married Filing Separately";
			else if (enter == 4)
				output = "Head of Household";
			else;
			
			return arrayOfValues;
			
			
		}
		
		public static double[ ]  computeIncometax(double[ ] arrayofNumbers, double taxAmount, double enter, double tax1, double tax2, double tax3, double tax4, double tax5, double tax6, double taxfinal1, double taxfinal2, double taxfinal, double alphatax, double betatax)
		{
			tax1 = 11158.50 + 0.31*(taxAmount - 49300.00);
			tax2 = 5100.00 + 0.28*(taxAmount - 34000.00);
			tax3 = 18582.00 + 0.31*(taxAmount - 82150.00);
			tax4 = 9291.00 + 0.31*(taxAmount - 41075.00);
			tax5 = 4095.00 + 0.28*(taxAmount - 27300);
			tax6 = 16177.00 + 0.31*(taxAmount - 70450);
			
			
			
			if (taxAmount > 34000 && taxAmount < 82150)
				taxfinal1 = tax2;
			else 
				taxfinal1 = tax3;
			
			if (taxAmount > 27300 && taxAmount < 70450)
				taxfinal2 = tax5;
			else
				taxfinal2 = tax6;
			
			if (enter == 1)
				taxfinal1 = tax1;
			else if (enter == 2)
				alphatax = taxfinal1;
			else if (enter == 3)
				taxfinal2 = tax4;
			else if (enter == 4)
				betatax= taxfinal1;
			else;
			
			if (enter == 1)
				taxfinal = taxfinal1;
			else if (enter == 2)
				taxfinal = taxfinal1;
			else if (enter == 3)
				taxfinal = taxfinal2;
			else if (enter == 4)
				taxfinal = taxfinal2;
			else;
			
			return arrayofNumbers;
				
		}
		
	public static void displayFinal(double taxAmount, String output, double taxfinal)
		{
			
			NumberFormat nf = NumberFormat.getNumberInstance( );	// Defines the number format.
			nf.setMinimumFractionDigits(2);							// Sets the minimum decimal places.
			nf.setMaximumFractionDigits(2);							// Sets the maximum decimal places.
			
			// Display the final statement
			System.out.println(" ");
			
			System.out.println("   Taxable Income Amount" + "	     Filing Status" + "	         Tax Amount" );
			System.out.println("-------------------------------------------------------------------------------");
			System.out.println("        $" + nf.format(taxAmount) + "               " +  output + "       $" + nf.format(taxfinal));
			
			
			System.out.println(" ");
		}
	}

And yes....I did receive the unreachable code error.

If you carefully check the following piece of code :-

String output = null;
String menu = null;
Scanner keyboard = new Scanner (System.in);
System.out.print("\nEnter your taxable income amount: $");
do {
  taxAmount = keyboard.nextInt( );	// Stores the chosen movie.
  validInput = true;
  if ( (taxAmount < 50000) ) {
    System.out.print ("\n(You must an amount greater than or equal to $50,000): $");
    validInput = false;						 
  }
}while(!validInput);
			
// A method call to displayMenu
displayMenu (menu, validEnter, enter, output);
			
// A method to call to computeIncometax
computeIncometax (taxAmount, enter, tax1, tax2, tax3, tax4, tax5, tax6, taxfinal1, taxfinal2, taxfinal, alphatax, betatax);
			
// A method to call to displayFinal
displayFinal (taxAmount, output, taxfinal);

From the start to the end when you call the method displayFinal(). the variable "output" is never initialized (Apart from being initialised to null at declaration time), which is why "null" is getting printed.

I am not sure then what to initialize my String output to. I am lost.

I am not sure then what to initialize my String output to. I am lost.

Ehh !!! Its your program you tell me what you wanted to be printed instead of "null" !!!

I want the output depending on which value they chose (1-4) and the final tax amount depending on which output they chose.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.