943,752 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 749
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 23rd, 2009
0

Output and null

Expand Post »
Hello. I have written the code below but when I compile my program, I get

Taxable Income Amount Filing Status Tax Amount
-------------------------------------------------------------------------------
$50,000.00 null $0.00

I initialized my String output as null but I do not know how to initialize it otherwise. And I need my output to be called back to the main method in order to compute taxfinal. Can someone please help?

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


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;
			double enter = 0;
			String output = null;
			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(" ");
		}
	}
Reputation Points: 10
Solved Threads: 0
Light Poster
olgratefuldead is offline Offline
29 posts
since Feb 2009
Feb 23rd, 2009
0

Re: Output and null

you are passing by value, therefore any changes made to "output" are only made within the method.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 23rd, 2009
0

Re: Output and null

I am confused by what you mean. Can you possible explain further. I am very new to Java. Thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
olgratefuldead is offline Offline
29 posts
since Feb 2009
Feb 23rd, 2009
0

Re: Output and null

java Syntax (Toggle Plain Text)
  1. {
  2. int var = 0;
  3. somemethod(var);
  4. print var;
  5. }
  6.  
  7. somemethod(int var) {
  8. var = 5;
  9. }

this is just pseudo code. in this example, the output of the code (printing var) will NOT be 5, but 0. the value of var is passed to the method, and so the method can use this, but any modifications to var are only valid within the method, it doesn't change var of the caller.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 23rd, 2009
0

Re: Output and null

So I think my next question is how do I get the value of output back to the caller method. Thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
olgratefuldead is offline Offline
29 posts
since Feb 2009
Feb 23rd, 2009
0

Re: Output and null

You only need to return a single value from that method: the value the user selected from the menu.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Feb 23rd, 2009
0

Re: Output and null

I tried what you said but I'm still getting the same output. 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;
			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;
			double enter = 0;
			String output = null;
			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 double 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 enter;
			
			
		}
		
		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 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(" ");
		}
	}
Reputation Points: 10
Solved Threads: 0
Light Poster
olgratefuldead is offline Offline
29 posts
since Feb 2009
Feb 23rd, 2009
0

Re: Output and null

you are returning the value, but doing nothing with it. another question, are you actually returning the correct value?
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 23rd, 2009
0

Re: Output and null

Well it seems that I am returning 'enter' so that should be stored in the calling method and I changed the second return to 'taxreturn'. So that should call enter from the calling method and return taxreturn up to the top and then should display it on the bottom. I think my logic is probably off.
Reputation Points: 10
Solved Threads: 0
Light Poster
olgratefuldead is offline Offline
29 posts
since Feb 2009
Feb 24th, 2009
0

Re: Output and null

what is the point in returning "enter" when all your logic related to "enter" is already complete within the method? and as i said previously, simply returning a value is useless unless you actually use it (e.g. assign it to something...).
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help with java program
Next Thread in Java Forum Timeline: Approxiamation Program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC