Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8750209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:49:38+00:00 2026-06-13T12:49:38+00:00

I have a project where I have to write a program that prompts the

  • 0

I have a project where I have to write a program that prompts the user for an initial investment amount and a goal investment amount and calculate how many years it will take to grow from the initial amount to the goal amount with a fixed interest rate (ie: 5 %). (use the WHILE loop). Print out the results from each year. For example, if you chose to invest $1,000 for 5 years:
Year 1 1005
Year 2 1011
Etc:

I was able to compile the java program but I was only able to prompt the user for an initial investment and goal investment with a 5% interest. The output was incorrect. What am I doing incorrect? Here is my code.

import static java.lang.System.out;
import java.util.Scanner;
import java.io.*;

public class Investment_Calculator {//main
public static void main(String[] args) {//begins body

double principal = 1;//initial amount investing
double interest = 1;
double rate = 0.05;//the fixed interest amount
int years = 1;//amout of years it will take to achieve goal
double goal = 1;
double total = 1;

Scanner myScanner = new Scanner(System.in);

System.out.println("*************************************** ");
System.out.println("* Welcome to the Investment Calculator * ");
System.out.println("*************************************** ");

System.out.println ("Enter your initial investment amount: if you want to exit enter   0.");
int inputNumber = myScanner.nextInt();
principal = inputNumber; 

if (inputNumber == 0){//if num = 0 exit class

System.exit(0);
}

System.out.println ("Enter your goal investment amount: ");
int inputNumber2 = myScanner.nextInt ();
goal = inputNumber2;

System.out.println ("The fixed interest rate is 5%");

total= principal; // start with our initial amount of money
for (; total < goal; total= (1+interest)*total);   

{   System.out.print("The number of years you must invest to meet your goal of $ ");
 System.out.print(goal);
 System.out.print(" is");   
 System.out.println(years);
 System.out.println(" years");

} 
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:49:39+00:00Added an answer on June 13, 2026 at 12:49 pm
    for (; total < goal; total= (1+interest)*total);
    

    You have a semi-colon at the end of this loop. Remove it.

    Because of that, your next set of print statements after for loop becomes an unnamed block: –

    {   
     System.out.print("The number of years you must invest to meet your goal of $ ");
     System.out.print(goal);
     System.out.print(" is");   
     System.out.println(years);
     System.out.println(" years");
    
        years++;  <-- // Increment `years` here.
    } 
    

    And those will be executed only once.


    UPDATE: –

    You can use the below code to get the totalInterest for each year and the final output

    int years = 1;
    while (principal <= goal) {
        double totalInterest = principal * rate * years / 100.0;
        principal = principal + totalInterest;
    
        System.out.println("Interest amount for year: " + years + 
                           " = " + totalInterest);
    
        years++;
    }
    
    System.out.print("The number of years you must invest to meet your goal of $ ");
    System.out.print(goal);
    System.out.print(" is");   
    System.out.println(years);
    System.out.println(" years");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC project that I would like to write in .NET 4.0
Now I have a python project, I write my unit-testing code in many of
My current project is to write a program that basically is a cash register
I need to write a program (a project for university) that solves (approx) an
I have a program below that tries to take input from the user and
As a part of my current pet project, I have to write an program
Some explanation: for a project I'm working on I have to write a program
This is a test program that I have written for a larger project that
I am trying to write a program that will take the users input value
I have a school project in which we're going to write a financial engine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.