Having some difficulties getting this assignment to work. What, I'm having trouble completing is creating an conditional statement that calculates interest for the amount of years entered by the user at the interest rate they enter and displays it in a message box.

This is my code so far:

import javax.swing.JOptionPane;
import javax.swing.JOptionPane;
public class Investment
{
	public static void main(String[] args)
	{

		String inputInvest;
		double invest;

		String inputYears;
		int years;

		String inputRate;
		double rate;
		
		int total

		JOptionPane.showMessageDialog(null, "Diamond Frizzell\n" + "Assign 4,ITMP 2650\n" + "Java Programming, Spring 2012 CRN 11988\n" + "Due Feb 22,2012");

		inputInvest = JOptionPane.showInputDialog(null, "Enter investment amount");
		invest = Double.parseDouble(inputInvest);


		inputYears = JOptionPane.showInputDialog(null, "Enter number of years");
		years =Integer.parseInt(inputYears);


		inputRate = JOptionPane.showInputDialog(null, "Enter interest rate (as a decimal)");
		rate = Double.parseDouble(inputRate);


		total =	JOptionPane.showMessageDialog(null, "Your investment at " + invest + total);
	}
}
}

I think my loop statement should look like this (see below), but I'm not sure. A little guidance or input would be great. Thanks! I also thing my last, JOption statement isn't right. Well, pretty much I know it isn't right because I get errors when I try and run it.

Loop:

while (year <= years) 
{
total = invest * years * rate;
years++;

So, it is for simple interest or compunded interest? The calculation is pretty different for both of them, see. If you would explain further...

And as for the JOptionPane.showMessageDialog() part, try this code: (Replace your text with it's text)

JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
Member Avatar for ztini
public class Interest {
	
	public static double compound(double principal, double rate, int periods) {
		return principal * Math.pow(1 + rate / 100, periods);
	}
	
	public static double simple(double principal, double rate, int periods) {
		return principal * rate * periods;
	}
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.