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

Thread: "Variable not initialized"

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "Variable not initialized"

    This is a class that I made to test my if-else statement for calculating withholding tax for gross pay. The error I get is this:
    error: variable tax might not have been initialized
    System.out.println("Withholding Tax = " + tax);


    I initialized the variable tax and covered all of the possible values for gross in the if-else statements. So what am I doing wrong?

    import java.util.Scanner;
    class apples
    {
    	public static void main(String args[])
    	{
    		Scanner input = new Scanner(System.in);
    		double gross = input.nextDouble();
    		double tax;
     
    		if (0 <= gross && gross <= 300.00)
    		{
    			tax = 0.10;
    		}
    		else if (300.01 <= gross && gross <= 400.00)
    		{
    			tax = 0.12;
    		}
    		else if (400.01 <= gross && gross <= 500.00)
    		{
    			tax = 0.15;
    		}
    		else if (gross >= 500.01)
    		{
    			tax = 0.20;
    		}
    		else
    		{
    			System.out.println("ERROR! Gross pay is less than zero");
    		}
     
    		System.out.println("Withholding Tax = " + tax);
    	}
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: "Variable not initialized"

    Hello davis220!
    You have not initialized tax, you have declared it
    double tax;
    You should just give it an initial value (0 is very common for a double).

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. Replies: 10
    Last Post: October 26th, 2011, 02:22 PM
  3. Replies: 4
    Last Post: October 20th, 2011, 11:26 AM
  4. Is there an integer-type variable bigger than "long"?
    By bardd in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 02:43 PM
  5. Replies: 3
    Last Post: April 20th, 2009, 08:35 AM