# Homework help: looping problems

• 02-28-2012, 07:27 PM
CaptainLaurie
Homework help: looping problems
Hi,

My homework this week is to create a program that shows the return on an investment. It must ask the user to input the amount of the investment, the number of years, and the interest rate. (if a zero is entered, an error message must instruct the user that was an invalid input) I need the program to then print out the amount of the investment year by year, ie, my output should look like this. I've put in random numbers as an example:

Enter investment amount: 100

Enter number of years: 3

Enter interest rate (as a decimal): .05

Your investment at 5.0 percent is:

After year 1 \$105.00
After year 2 \$110.25
After year 3 \$115.76

And so on, depending on the number of years the user entered at the prompt. I'm having trouble with a) getting the output to print by year instead of as one lump sum, and b) how to define a number in the loop that I don't know yet. I want to use "final int MAX" for that, but I'm not sure how.

I'm also having a problem getting the "not enough information message" to display without the investment output statement printing out afterward. Should I use an else statement for the output? I keep getting problems when I try to do that.

Can anyone help me out with some advice? My code is below.

import java.text.*;
import java.util.*;
public class Investment {

public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(2);

double investment;
double growth;
double rate;
int years;

Scanner input = new Scanner(System.in);

System.out.print("Enter investment amount: ");
investment = input.nextDouble();

System.out.print("Enter number of years: ");
years = input.nextInt();

System.out.print("Enter interest rate (as a decimal): ");
rate = input.nextDouble();

if (investment <= 0||years <=0 ||rate<=0)
{
System.out.println("Not enough information - amount must be non-zero.");
}

growth = investment * rate * years;

System.out.println("Your investment at " + rate + " is " + df.format(growth));

}
}
• 03-02-2012, 03:04 PM
RonnieVDPoel
re: homework
Here's what I came up with... The start of your code stays the same.
Code:

```if (investment <= 0||years <=0 ||rate<=0) {     System.out.println("Not enough information - amount must be non-zero."); } // this loop will provide the individual return per year for (int i = 1; i <= years; i++){     double profit = investment * rate;  // profit made this year     investment += profit;                // add this profit to the initial investment     System.out.println("Your investment after " + i + " year at " + rate + " is " + df.format(investment));  }```
note that the user input should always be with a ',' and not a '.' else you will get an input error. Maybe you should try and implement a method that checks the user input.

hope this helps.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.07636 seconds
• Memory Usage 2,354KB
• Queries Executed 11 (?)
Template Usage (20):
• (1)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates