I am having problems with it continuous loop at the end of the program. Any help would be appreciated. Thank you. Below is the coding so far.

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

public class Investment {
public static void main(String[] args) {

double principal = 0;
double interest = 0;
double rate = 0.05;
int years = 0;
double goal = 0;
double total = 0;

Scanner myScanner = new Scanner(System.in);

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


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

// Decision for exit
if (input1 == 0){
System.exit(0);
}

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

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

 // Loop to get total years
 total= principal;
 total= (interest+1)*total;
    for (years=0; total < goal; years++){
       System.out.print("The number of years you must invest to meet your goal of $ " + goal + " is " + years + " years.");

    }
  }

}

Edited 6 Years Ago by Krazykado: n/a

See closely here:

for (years=0; total < goal; years++){
       System.out.print("The number of years you must invest to meet your goal of $ " + goal + " is " + years + " years.");

    }

years is declared to be the control variable, though it is never tested if years meets certain condition also, in the loop total and goal are never changes =>
the loop will never end. You have to check your logic again.

Ok got the program working. Now I can't get the question at the end do you want to enter another amount and the loop to run again. I don't think my brain is working. Thanks for any help. Here is what I have.

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

public class Investment {
public static void main(String[] args) {

double principal = 0;
double interest = 0;
double rate = 0.10;
int years = 0;
double goal = 0;
double total = 0;
int userinput = -1;

Scanner myScanner = new Scanner(System.in);

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


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

// Decision for exit
if (input1 == 0){
System.exit(0);
}

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

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

 // Loop to get total years
 total= principal;
 total= (interest+1)*total;
    for (total = 0; total < goal; total++){
       System.out.println("To reach your $" + goal + "0 goal it will take " + years + " years.");
       years++;

 // Continue play
 	if (total == goal){
 		System.out.println("Would you like to enter another amount?");
 			int input3 = myScanner.nextInt ();

 		} while (userinput == 1);
    }
  }

}

Ok got the program working. Now I can't get the question at the end do you want to enter another amount and the loop to run again. I don't think my brain is working. Thanks for any help. Here is what I have.

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

public class Investment {
public static void main(String[] args) {

double principal = 0;
double interest = 0;
double rate = 0.10;
int years = 0;
double goal = 0;
double total = 0;
int userinput = -1;

Scanner myScanner = new Scanner(System.in);

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


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

// Decision for exit
if (input1 == 0){
System.exit(0);
}

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

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

 // Loop to get total years
 total= principal;
 total= (interest+1)*total;
    for (total = 0; total < goal; total++){
       System.out.println("To reach your $" + goal + "0 goal it will take " + years + " years.");
       years++;

 // Continue play
 	if (total == goal){
 		System.out.println("Would you like to enter another amount?");
 			int input3 = myScanner.nextInt ();

 		} while (userinput == 1);
    }
  }

}

Change the for loop to total <= goal.

Thanks for that contribution brycematheson.

Just two quick comments on the code:

Your naming convention is perfectly valid, but not one you will often find in Java - the source code of the API itself is an excellent reference for "standard" coding style in general.

You have two buttons that do two completely different things, yet you create a single listener that then has to use a switch to determine which button was pressed in order to take the appropriate action. That's really messy compared to having two simple listeners. That's even more relevant if you are using a current version of Java, in which you can express those two listeners as trivial lambdas

jbtCompute.addActionListener(this::computeValue);
jbtReset.addActionListener(this::resetForm);
// you'll simply need to add an ActionEvent parameter to those two methods
This question has already been answered. Start a new discussion instead.