As you are about to find out, I'm just picking up programming and I have a book in which I'm trying to complete a problem. I believe I'm having scope problems which I don't understand any assistance would be greatly appreciated. I'm trying to take in entered data which is self explanatory if you read the dialogues. But I get the input, and i cant seem to use the p1area and p2area variables outside of the if-statements. I want to use them outside because I need to use them in a final output which summarizes all the data entered. I don't know if any of what I'm typing is enough to get assistance, but if you think you can see anything which is going wrong, please don't hesitate to respond. Thank you.
import javax.swing.JOptionPane; public class CopyOfPizzas { public static void main(String[] args) { double p1area; double p2area; String input1 = null; String input2 = null; String shape = null; String shape2 = null; input1 = JOptionPane.showInputDialog("shape of pizza, R or C (Q to quit)"); if(input1 == null || input1.equals("q") || input1.equals("Q")) { return; } shape = input1; if (shape.equals("r") || shape.equals("R")) { p1area = calc_rectangle(); } if (shape.equals("c") || shape.equals("C")) { p1area = calc_circle(); } R or C is input earlier and then the other input is retrieved when calc_rectangle/calc_circle is called and it returns a value and assigned to p1area...once i get that assigned back in the if-statement above this comment i want to use p1area outside of the if-statement in a JOptionPane.showMessageDialog() with some other details. When i try to use it outside of the if-statement it says the local variable may not have been initialized input2 = JOptionPane.showInputDialog("shape of pizza, R or C (Q to quit)"); if(input2 == null || input2.equals("q") || input2.equals("Q")) { return; } shape2 = input2; if (shape2.equals("r") || shape2.equals("R")) { p2area = calc_rectangle(); } if (shape2.equals("c") || shape2.equals("C")) { p2area = calc_circle(); } JOptionPane.showMessageDialog(null, "Pizza one area: " + p1area + "\nPizza two area: " + p2area); } public static double calc_rectangle() { String temp1 = JOptionPane.showInputDialog("length in inches: "); String temp2 = JOptionPane.showInputDialog("width in inches: "); Double length = Double.parseDouble(temp1); Double width = Double.parseDouble(temp2); double area = length * width; return area; } public static double calc_circle() { String temp1 = JOptionPane.showInputDialog("diameter in inches"); double diameter = Double.parseDouble(temp1); double area = (3.14 * (Math.pow(diameter/2, 2))); return area; } }