We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,627 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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

4
Contributors
2
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
3
Views
gerchi152
Light Poster
37 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

stultuske
Industrious Poster
4,366 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 23

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.

Ezzaral
null
Moderator
16,110 posts since May 2007
Reputation Points: 3,292
Solved Threads: 873
Skill Endorsements: 27

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0659 seconds using 2.74MB