1,105,229 Community Members

"variable might not have been initialized" but my variable is initialized already

Member Avatar
gerchi152
Light Poster
37 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm getting this error variable might not have been initialized on ch2 and ch3:

import javax.swing.JOptionPane;

public class calculatorJava
{
	public static void main(String[] args)
	{
		String ch, ch2, ch3, add1, add2, sub1, sub2, mul1, mul2, div1, div2, choice1, exp;
		int chans, chans2, chans3, addo1, addo2, subO1, subO2, mulo1, mulo2, divo1, divo2, exp1, conas=0, choic1;
		double adde, sube, mule, dive, expAns;
		JOptionPane.showMessageDialog(null, "Calculator");
		
		//selecting level
		ch=JOptionPane.showInputDialog("Choose level: \n press 1 for simple math \n press 2 for other functions:");
		chans=Integer.parseInt(ch);
		
		if (chans==1) //for simple math
		ch2=JOptionPane.showInputDialog("Choose operations: \n press 1 for add \n press 2 for subtract \n press 3 for multiply \n press 4 for divide");
		chans2=Integer.parseInt(ch2);
		if (chans2==1) //add
		do
		{
			add1=JOptionPane.showInputDialog("Input first number:");
			addo1=Integer.parseInt(add1);
			
			add2=JOptionPane.showInputDialog("Input second number:");
			addo2=Integer.parseInt(add2);
			
			adde=addo1 + addo2;
			JOptionPane.showMessageDialog(null, "Answer: " + addo1 + " + " + addo2 + " = " + adde);
	}while(conas==1);
	else if (chans2==2) //subtract
	do
		{
			sub1=JOptionPane.showInputDialog("Input first number:");
			subO1=Integer.parseInt(sub1);
			
			sub2=JOptionPane.showInputDialog("Input second number:");
			subO2=Integer.parseInt(sub2);
			
			sube = subO1 - subO2;
			
			JOptionPane.showMessageDialog(null, "Answer: " + subO1 + " - " + subO2 + " = " + sube);	
	}while(conas==1);
	else if (chans2==3) //multiply
	do
		{
			mul1=JOptionPane.showInputDialog("Input first number:");
			mulo1=Integer.parseInt(mul1);
			
			mul2=JOptionPane.showInputDialog("Input second number:");
			mulo2=Integer.parseInt(mul2);
			
			mule = mulo1 * mulo2;
			
			JOptionPane.showMessageDialog(null, "Answer: " + mulo1 + " * " + mulo2 + " = " + mule);	
	}while(conas==1);
	else if (chans2==4) //divide
	do
		{
			div1=JOptionPane.showInputDialog("Input first number:");
			divo1=Integer.parseInt(div1);
			
			div2=JOptionPane.showInputDialog("Input second number:");
			divo2=Integer.parseInt(div2);
			
			dive = divo1 / divo2;
			
			JOptionPane.showMessageDialog(null, "Answer: " + divo1 + " / " + divo2 + " = " + dive);	
	}while(conas==1);
	
	//for other function
	else if (chans==2)
	ch3=JOptionPane.showInputDialog("Choose operations: \n press 1 for exponent \n press 2 for binomial \n press 3 for quadratic function");
	chans3=Integer.parseInt(ch3);
	if (chans3==1)
	do
	{
		exp=JOptionPane.showInputDialog("x^2 \n Input a number for x:");
		exp1=Integer.parseInt(exp);
		expAns = exp1 * exp1;
		JOptionPane.showMessageDialog(null, "Answer: " + expAns);
	}while(conas==1);
	System.exit(0);
}
}

any asap help is much appreciated

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

they are only initialized if you actually run that if block, so, they're not always initialized.

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

Some of your variables will only be initialized if conditions are met. The compiler requires that they be initialized under all conditions.

If you declare them to have an initial value, the compiler will be satisfied

String ch2="";

Edit: cross-posted with above.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article