I keep getting an error variable F3 may have not been intialized on the last line of code you see.
What am I doing wrong?
{
Float F1,F2, F3;
F1 = Float.parseFloat(
JOptionPane.showInputDialog("Enter a number and press ok."));
F2 = Float.parseFloat(
JOptionPane.showInputDialog("Enter a second number and press ok."));
if(F1 >= F2)
{
F3=(F1 * F2) + (F1 * 2);
}
if(F2 >= F1)
{
F3 =(F1 + F2) + (F2 * 5);
}
DecimalFormat dec = new DecimalFormat("##.##");
JOptionPane.showMessageDialog(null,"Your calculations are:" +(F3),"Caculations", JOptionPane.INFORMATION_MESSAGE);
As per JLS:
Each local variable and every blank final field must have a definitely assigned value when any access of its value occurs.
Additionally from §14.4.2:
If a declarator does not have an initialization expression, then every reference to the variable must be preceded by execution of an assignment to the variable, or a compile-time error occurs.
With the code there, it is possible that nothing ever gets assigned to F3
before it is used (in the last line of the code snippet).
Use F3 = null
.