-1

I need to add for the decimal placement, which I know how to write that. I've tinkered with this code and tried different thing, which is why my else statement isn't the same as the if.

Plus I Will either go scanner over GUI. Anyways, when I chose the if statement then it shows the results in the console, but then goes to the else statement. I've tried several things, but can't figure out why.

import java.text.DecimalFormat;

import javax.swing.*;

public class MathFormula {

    public static void main(String args[]) {

        //Declarations
        double getNumDouble;
        double volumeSphere;
        String getNum;
        double area;
        double volume;
        String getUserDecision; //Declared getUserDecision for outer loop decision
        DecimalFormat df = new DecimalFormat("####0.000");
        java.util.Scanner input = new java.util.Scanner(System.in);
        //End Declarations

        // Greeting
        greeting();

        System.out.println("  "); //Left blank for clarity

        // Ask user to start the program (the outer loop)

        getUserDecision = decisionOuter();
        while (getUserDecision.equalsIgnoreCase("Y")) {

            getUserDecision = decisionInner();

            if (getUserDecision.equalsIgnoreCase("S")) {

                System.out.print("Enter you number for volume of sphere: ");
                getNum = input.next();
                getNumDouble = Double.parseDouble(getNum);
                volSphere(getNumDouble);

            } else if (getUserDecision.equalsIgnoreCase("C"))
                System.out.println(" ");
            area = arCircle();
            System.out.println("you have worked: " + df.format(area));

            getUserDecision = decisionOuter();
        }

        goodbye();
    }

    public static String decisionOuter() //Outerloop decision
    {
        String userDecisionString;

        userDecisionString = JOptionPane.showInputDialog("Would you like to use the program Y/N: ");

        return userDecisionString;
    }

    public static String decisionInner() //Inner loop decision
    {
        String userDecisionString2;

        userDecisionString2 = JOptionPane.showInputDialog("C or S: ");

        return userDecisionString2;
    }

    public static void volSphere(double getNumDouble) //Vol formula
    {
        double volumeSphere;

        volumeSphere = (4 * Math.PI * getNumDouble * getNumDouble * getNumDouble) / 3;

        System.out.println("entered " + getNumDouble + "returned" + volumeSphere);
    }

    public static double arCircle() //area formula
    {
        double getNum;
        double areaCircle;
        String getNumString;

        getNumString = JOptionPane.showInputDialog("Enter you number for radius of circle: ");
        getNum = Double.parseDouble(getNumString);
        areaCircle = (getNum * getNum * Math.PI);
        return areaCircle;
    }

    public static void greeting() //greeting
    {
        final String msgGreet = "Welcome.";
        System.out.println(msgGreet);
    }

    public static void goodbye() //goodbye
    {
        final String msgBye = "goodbye.";
        System.out.println(msgBye);
    }
}
Michael
  • 41,989
  • 11
  • 82
  • 128

1 Answers1

1

because you havent included the curley braces around the else inclusion. its only considering the 'System.out.println(" ");' as your else statement, everything else below it is executed, if you want exclusion then include curley braces around the code you want in the else condition

//...
}
else if (getUserDecision.equalsIgnoreCase("C"))
{
    System.out.println(" ");
    area = arCircle();
    System.out.println("you have worked: " + df.format(area));
}

getUserDecision = decisionOuter();
//...
Michael
  • 41,989
  • 11
  • 82
  • 128
andrew.butkus
  • 777
  • 6
  • 19
  • You don't know if the else containg only `System.out.println(" ");` is intentional. – aalku Nov 16 '17 at 16:50
  • @Bohemian Incorrect. Indentation *potentially implies* this. The wider context would suggest it's **not** intentional. S = volume for Sphere, C = area for circle. – Michael Nov 16 '17 at 16:51
  • That was it. I can't believe I overlooked delimiters on the else statement. Thank you – james mckenzie Nov 16 '17 at 16:57