java newbie question method headers and if statements
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
java newbie question method headers and if statements
I am currently enrolled in a basic on-line community college course and I desperately need help before pulling out the rest of my hair. I have contacted the instructor and tried the tutors at school to no avail.
I am sure this is probably a very simple fix, but I just cant see it. The exercise involved writing a program using a specific method header and formula for after getting the input from a user of initial investment and annual interest, the program is to display a table showing the year and investment value for years 1 - 30. Also to be displayed at the beginning is the initial investment value, and interest year.
After many unsuccessful attempts, where I would get class or interface expected errors, I was finally able to compile and run. Only, the results is not what I need. Can anyone steer me in the right direction? Many thanks
public class Value {
/**main method*/
public static void main(String[] args) {
//Prompt the user to enter the initial investment amount
String investmentAmountString = JOptionPane.showInputDialog (null, "Enter the Investment Amount,"+
"\nfor example 1500.50", "Programming Exercise 4.3 Input", JOptionPane.QUESTION_MESSAGE);
//Change the string into a double value
double investmentAmount = Double.parseDouble(investmentAmountString);
//Prompt the user to enter the yearly interest rate
String annualInterestRateString = JOptionPane.showInputDialog (null, "Enter the Interest Rate," +
"\nfor example 9.25", "Programming Exercise 4.3 Input", JOptionPane.QUESTION_MESSAGE);
//Change the string into a double value
double annualInterestRate = Double.parseDouble(annualInterestRateString);
/**Return the table for the investment amount value over the 30 years*/
public static double futureInvestmentValue (double investmentAmount, double monthlyInterestRate,
int years){
You are way mixing up your while loop there. What you apparently intended to do
was to calculate and print the value for each year. What you are in fact doing is calculating the value for one year, incrementing the variable years 30 times, printing out the value for that first year, and returning. In short, your brace structure is messed up. Move the calculation inside the while loop and add some nice braces.
I have a similar problem I have been working on, and have gotten the results to display in the DOS window, but would like to instead have them display in the JOptionPane. I know you need to use and empty string but I'm not sure how to implement this. I think I could replicate the process if I saw how you would do it in this particular case (using the JOptionPane for the code posted by Juvestar15). Can anyone give me some hints?
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Hi georacer. Welcome to LQ.
Your problem is very likely to be unrelated so instead of reviving a 2 years old thread, please start a new one, and post some code showing where your problem is.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.