Search code examples
javaswingparsingmethodsjoptionpane

Convert string to double in a dialog box


I need help to understand the code.

public class ComputeLoanUsingInputDialog{
    public static void main(String[] args){
        String annulInterestRateString = JOptionPane.ShowInputDialog("Enter annual interest rate:")

        // convert string to double
        double annualInterestRate = Double.parseDouble(annualInterestRateString);

        //Obtain monthly interest rate
        double monthlyInterestRate = annualInterestRate/1200;

        .....
    }
}

So my question is if you have a dialog box pop out to ask user to insert a number then why do you need to use parse method to convert the variable from string to double?


Solution

  • Because Java accepts the input as a String - it doesn't that what it's getting is a double until you tell it to parse that String into a double.