# Solved: "Variable might not have been initialized" errors



## technewbie1 (May 12, 2006)

Good Morning,
Thank you in advance for your help.

I am in a beginning Java programming course and need some guidance in understanding why Im receiving variable might not have been initialized errors.

I need to create a TaxReturn class with fields for SSN, last name, first name, address, city, state, zip, annual income and marital status. In the class I must create a constructor that requires arguments for all the fields. The constructor also calculates the tax liability. Lastly, I must include a display method that returns all the TaxReturn data.

Thank you again,
newbie1

Two files are enclosed below (TaxReturn.java and PrepareTax.java (which prompts the user to create a return and displays the input values).

```
import javax.swing.*;
public class TaxReturn
{
	private String socialSecurity;
	private String sS, modSS;
	private String lastName;
	private String firstName;
	private String streetAddress;
	private String city;
	private String state;
	private String zipcode;
	int zCode;
	private String annualIncome;
	double income;
	private String maritalStatus;
	double taxDue;
	int taxPercent;
	
	int x, y;
	
	public TaxReturn()
	{
		String socialSecurity, modSS, lastName, firstName, streetAddress, city, state, zipCode, annualIncome, maritalStatus;
		String sS, lN, fN, sA, c, s, z;
	 
		double income;
		int mStatus;
		int ss;
		
		
		System.out.println("Social Security Number:\t" + socialSecurity + "\nLast Name:\t" +
			lN + "First Name:\t" + fN + "Street Address:\t" + sA + "City:\t" + c +
			"State:\t" + s + "Zip Code:\t" + z + "Annual Income:\t" + income + "Marital Status:\t" +
			mStatus + "Tax Due:\t" + taxDue); 
		
		
		if(income <= 20000 && income >= 0)
					taxPercent = 15;
		if(income > 20000 && income < 50001)
			{
				for(mStatus	= 2;;) 						// Marital Status = 2 = Single
					taxPercent = 22;
													// Marital Status = 1 = Married 
					taxPercent = 20;
			}
		if(income >= 50001)
		{		
				for(mStatus = 2;;)
					taxPercent = 30;
				
					taxPercent = 28; 
		}
		taxDue = taxPercent * income;
	}
}

import javax.swing.JOptionPane;
public class PrepareTax
{
	public static void main(String[] args)
	{
		String socialSecurity, lastName, firstName, streetAddress, city, state, zipCode, annualIncome, maritalStatus;
		String part1, part2, part3, modSS;
		int zip, stringLength, x, zipLength, annualIncomeLength;
		
		char c;
		int i;
		int ss;
		
		socialSecurity = JOptionPane.showInputDialog(null, "Please enter your social security number as " +
			"xxx-xx-xxxx");
		stringLength = socialSecurity.length();
		part1 = socialSecurity.substring(0, 2);     // Eliminating the hyphens
		part2 = socialSecurity.substring(4, 5);
		part3 = socialSecurity.substring(7, 10);
		modSS = (part1 + part2 + part3);             // Concatenating the substrings (Do I instead need to append/concatenate via StringBuffer class?)
		ss = Integer.parseInt(modSS);						// Converting the modified SSN (SSN w/o hyphens) to an integer.
		
		if((stringLength != 11) || (socialSecurity.startsWith("000")) || (socialSecurity.charAt(3)!= '-') || (socialSecurity.charAt(6)!= '-'))
		 		socialSecurity = JOptionPane.showInputDialog(null, "Invalid." +
					 "\nPlease re-enter SSN.");
		else if((ss < 1000000) || (ss > 999999999))
				socialSecurity = JOptionPane.showInputDialog(null, "Invalid." +
					 "\nPlease re-enter SSN.");
		else
			System.out.println("Social Security Number: " + socialSecurity); // Does it make more sense to postpone the printing of all data until the end of the program? (Is that even possible considering the loops and their protected data?)
		
	 			
		lastName = JOptionPane.showInputDialog(null, "Please enter your last name.");
			System.out.println("Last name: " + lastName);
	   firstName = JOptionPane.showInputDialog(null, "Please enter your first name.");
			System.out.println("First name: " + firstName);
		streetAddress =JOptionPane.showInputDialog(null, "Please enter your street address.");
			System.out.println("Street Address: " + streetAddress);
	   city = JOptionPane.showInputDialog(null, "Please enter your city.");
			System.out.println("City: " + city);
		state = JOptionPane.showInputDialog(null, "Please enter your state.");
			System.out.println("State: " + state);
		zipCode = JOptionPane.showInputDialog(null, "Please enter your five digit zip code."); 
			zip = Integer.parseInt(zipCode);
			zipLength = zipCode.length();
		
			if((zipLength != 5) || (zip < 500 || zip > 99999))
			{
				zipCode = JOptionPane.showInputDialog(null, "Invalid." +
			"\nPlease re-enter Zip Code.");
			}
			System.out.println("Zip Code: " + zipCode);	
		
		maritalStatus = JOptionPane.showInputDialog(null, "Please enter your marital status as M (or m) for married  or S (or s) for single.");
			c = maritalStatus.charAt(0);
			
			if(maritalStatus.equalsIgnoreCase("M"))  
			{
				System.out.println("Marital Status: Married");
				maritalStatus = "1";
				int mStatus = Integer.parseInt("1");
			}		
			else	
				if(maritalStatus.equalsIgnoreCase("S"))
			{		System.out.println("Marital Status: Single");	
 					maritalStatus = "2";
					int mStatus = Integer.parseInt("2");
			}	
		annualIncome = JOptionPane.showInputDialog(null, "Please enter your annual income. (Round off to the nearest dollar. Do not include the $ sign.)");
			double income = Double.parseDouble(annualIncome);
			if(income < 0)
				System.out.println("Invalid entry. Please re-enter annual income.");
			else
				System.out.println("Annual income: " + income);
				
		
	}
}
```


----------



## DaBeers (Oct 31, 2008)

technewbie1 said:


> understanding why Im receiving variable might not have been initialized errors.


People here generally don't like helping students. It's not a crutch for making it to class. However, and I promise I never looked past the variables in your code, I might be able to give some advise.

This is a gimpy error in Java that other languages tolerate better. Just initialize your variables to a default value when their declared.

If you don't know what that means, take the F and start over because you have no buisness writing constructors.


----------



## Triple6 (Dec 26, 2002)

DaBeers said:


> People here generally don't like helping students. It's not a crutch for making it to class. However, and I promise I never looked past the variables in your code, I might be able to give some advise.
> 
> This is a gimpy error in Java that other languages tolerate better. Just initialize your variables to a default value when their declared.
> 
> If you don't know what that means, take the F and start over because you have no buisness writing constructors.


While we don't do posters homework for them we can still assist them or give them contrustive advice, unlike what you have done in your post. It may be wise for you to simply not post rather then to be rude to another number.


----------

