简体   繁体   中英

How to make string act like double

I believe I am correct by using Double.parseDouble, but I keep getting an error. What is the problem with the code? The lines where I keep receiving errors have asterisk around them.

import javax.swing.JOptionPane;

public class Interest {


public static void main(String[] args) {

// TODO Auto-generated method stub


//Enter Annual Interest Rate
String annualInterestRate1;
Double.parseDouble(annualInterestRate1);
JOptionPane.showInputDialog("Enter your 1st annual interest rate");

//Obtain Monthly Interest Rate
double monthlyInterestRate1;
**monthlyInterestRate1= annualInterestRate1 / 12;**


//Enter the Savings Amount
String savingsAmount1;
Double.parseDouble(savingsAmount1);
JOptionPane.showInputDialog ("Enter your 1st savings amount") ;

double endingBalance=0;



//Display Results
JOptionPane.showMessageDialog(null, "Adekunle Akinmola");
JOptionPane.showMessageDialog(null," ");

//Calculate Payment
for(int i =0; i<=5;i++){
    **endingBalance = savingsAmount1 * (1+ monthlyInterestRate1);**
    System.out.println("$"+ endingBalance);
}

}}

In this code

String annualInterestRate1;
Double.parseDouble(annualInterestRate1);
JOptionPane.showInputDialog("Enter your 1st annual interest rate");

You call the right methods, but you discard the result. Also you attempt to parse the input stream, before you have read it. I suggest you try.

String annualInterestRate1Str = 
    JOptionPane.showInputDialog("Enter your 1st annual interest rate");
double annualInterestRate1 = Double.parseDouble(annualInterestRate1Str);

You cannot divide a String by an integer.

Try this:

String annualInterestRate1 = "24";
double dblAnnualInterestRate1 = Double.parseDouble(annualInterestRate1);

//Obtain Monthly Interest Rate
double monthlyInterestRate1 = dblAnnualInterestRate1 / 12;

The problem is

String annualInterestRate1;/ this line you need to save the value for the string and then use that.
Double.parseDouble(annualInterestRate1); // this line you need to save the value for the double and then use that.

What you want to do is

//Enter Annual Interest Rate
String annualInterestRate1;
annualInterestRate1=JOptionPane.showInputDialog("Enter your 1st annual interest rate");
double annualInterestRate1d = Double.parseDouble(annualInterestRate1);

//Obtain Monthly Interest Rate
double monthlyInterestRate1;
monthlyInterestRate1= annualInterestRate1d / 12;

EDIT :

String savingsAmount1;
Double.parseDouble(savingsAmount1);
JOptionPane.showInputDialog ("Enter your 1st savings amount") ;

Needs to be changed to

String savingsAmount=JOptionPane.showInputDialog ("Enter your 1st savings amount") ;
double savingsAmount1 = Double.parseDouble(savingsAmount);

What do you think these 3 lines do?

String annualInterestRate1;
Double.parseDouble(annualInterestRate1);
JOptionPane.showInputDialog("Enter your 1st annual interest rate");

The first line declares a variable, but doesn't assign a value to it.

The second line will fail to compile, because annualInterestRate1 hasn't been "definitely assigned".
Calling parseDouble without using the return value is meaningless (unless you only did it to see if the parameter was a valid double value, which you're not).

The third line will show a dialog to the user, asking for input, but since you're not using the return value, whatever the user types is discarded.

Maybe you should read up on assignment operators: Assignment Statements in Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM