Kiril
Kiril

Reputation: 272

double + double = String?

I have a problem the output is supposed to be double but instead it is string I am trying to add two double values but it is giving it as a string. I am using eclipse. Currently the program is compiling and running. If anyone have a moment I would appreciate it.Cheers guys. Here is the source code.

import java.util.Scanner;

public class FutureInvestment 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter investment amount: ");
        double investmentAmount = input.nextDouble();

        System.out.println("Enter monthly interest rate: ");
        double monthlyInterestRate = input.nextDouble();

        System.out.println("Enter number of years: ");
        int numberOfYears = input.nextInt();

        double futureInterestValue = investmentAmount * ( Math.pow((1 + monthlyInterestRate), numberOfYears * 12));
        System.out.println("Accumulated value is: " + futureInterestValue  + investmentAmount);


    }

}

Upvotes: 1

Views: 233

Answers (9)

Simon Wang
Simon Wang

Reputation: 2943

I think change it to this would work:

    double futureInterestValue = investmentAmount * ( Math.pow((1 + monthlyInterestRate / 100), numberOfYears * 12));
    System.out.println("Accumulated value is: " + (futureInterestValue  + investmentAmount));

Upvotes: 1

Michael Madsen
Michael Madsen

Reputation: 55009

The problem is your number is getting way too large, and Java switches over to scientific notation when printing the value.

If your monthly interest rate is entered as 4.25 (meaning 4.25%), you have to convert that to the correct decimal representation of 0.0425 before using it in your calculations - you have to divide it by 100. If you don't, the interest rate used will be much larger than you intended; in this case 425%.

In other words, change

double monthlyInterestRate = input.nextDouble(); 

to

double monthlyInterestRate = input.nextDouble()/100; 

Upvotes: 0

emre nevayeshirazi
emre nevayeshirazi

Reputation: 19241

double accumulatedValue = futureInterestValue  + investmentAmount;
System.out.println("Accumulated value is: " + accumulatedValue);

Try this.

You were getting String as result of concatenation, since anything concatenated to a string is converted to string. Therefore, you need to complete the value beforehand as I shown above, or you need parentheses.

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

You need to format your output. You can use DecimalFormat or you can try the String#format function:

System.out.println(
    String.format("Accumulated value is: %.2f",
        futureInterestValue  + investmentAmount));

So you can get the 2 decimal output. Plus, I recommend to create a variable with your result, so you can turn your code into

double accumulatedValue = futureInterestValue  + investmentAmount;
 System.out.println(
    String.format("Accumulated value is: %.2f", accumulatedValue);

Upvotes: 2

Abdullah Md. Zubair
Abdullah Md. Zubair

Reputation: 3324

You can try:

System.out.println("Accumulated value is: " + (futureInterestValue + investmentAmount));

or add a variable like double accumulatedValue=futureInterestValue + investmentAmount; and then System.out.println("Accumulated value is: " + accumulatedValue);

Upvotes: 0

Edwin Buck
Edwin Buck

Reputation: 70909

When two operators could be evaluated within a line of code, they do so with a fixed precedence. While this example has been explained by many, you might want to review all of the precedence rules.

Upvotes: 0

Pradeep Gollakota
Pradeep Gollakota

Reputation: 2181

Since you're doing it in a println, it's doing string concatenation. If you want to add the double's together, you need to group them using ().

Try

System.out.println("Accumulated value is: " + (futureInterestValue  + investmentAmount));

Upvotes: 2

Steven
Steven

Reputation: 2477

System.out.println("Accumulated value is: " + (futureInterestValue  + investmentAmount));

After the first +, Java has concatenated the first string with the first double, resulting in a string. Then it does another concatenation with the second double. You need to calculate the result first before making a string out of it.

Upvotes: 0

Daniel Kurka
Daniel Kurka

Reputation: 7985

you are missing some brackets, so your statement gets executed from left to right, thus appending the double to the string. You would need something like:

System.out.println("Accumulated value is: " + (futureInterestValue + investmentAmount));

Upvotes: 0

Related Questions