A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

Java Help

928 views · started by cbkh2bankai ·
#1
Java Help
So I have to make this investment calculator which is posted down below me. After I compile and run it. The file seems to work perfectly fine except for one error. If I told it 100 for Initial Investment and then 500 for Investment goal. It says it would take 499 years. What did I do wrong?

/**
* @(#)Investment 2.java
*
*
* @author
* @version 1.00 2011/1/16
*/


import java.util.*;

public class Investment2 {
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 Beta");


// Enter initial amount
System.out.println ("Enter 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%"); //Change if you have different rate

// 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);
}
}

}
#2
Just from skimming over it:
// 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++;


You're assigning some value to "total" and then in the for loop you're replacing that value with 0. Maybe that's what's wrong?
#3
^ somewhat of what he just said. Asking for user input is easy. The algorithm is the tricky part. Clean up your un-needed variables. Your loop basically does nothing but count.

/**
* @(#)Investment 2.java
*
*
* @author
* @version 1.00 2011/1/16
*/


import java.util.*;

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

double principle = 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 Beta");


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


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

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


//System.out.println ("The fixed interest rate is 10%"); //Change if you have different rate

// Loop to get total years
total = principle;
for (int j = 0; total <= goal; j++)
{
total = total + total*.1;
if(total >= goal)
{
System.out.println("To reach your $" + goal + "0 goal it will take " + j + " years.");
}
}


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

} while (userinput == 1);
}
}


I feel a little proud of myself now :D