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

Recommended Answers

All 2 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.