2

I'm currently learning the Java language, which is my first programming language. I did research into arrays and was trying to make my program more efficient and simple with them. I realize there is a lot of repetitions in the original code. I appreciate any tips and criticism.

import java.util.Scanner;

public class Calculator {

static Scanner sc = new Scanner(System.in);//sets up scanner import

public static void main(String[] args) 
{
    double initalInvestment;
    double interestRate;
    int yearsInvested;
    int arrInt[] = new int[5];

    arrInt[0] = 1;

    int i = 1;
    while (i <= 4) {
        arrInt[i] = i + 1;
        i++;
    }

    System.out.println("Enter Inital Investment: ");
    initalInvestment = sc.nextDouble();

    System.out.println("Enter Interest Rate: ");
    interestRate = sc.nextDouble();

    System.out.println("Enter Years Invested: ");
    yearsInvested = sc.nextInt();

    double money1 = initalInvestment * interestRate;

    double money2 = money1 + initalInvestment;

    double y1 = money2;
    double y2 = y1 * interestRate + y1;
    double y3 = y2 * interestRate + y2;

    if (yearsInvested <= 1) 
    {
    System.out.println("Your Total for year: " + arrInt[0] + ", is " + y1 + "$");
    } else if (yearsInvested <= 2) {
        System.out.println("Your Total for year: " + arrInt[0] + ", is " + y1 + "$");
        System.out.println("Your Total for year: " + arrInt[1] + ", is " + y2 + "$");
    } else if (yearsInvested <= 3) {
        System.out.println("Your Total for year: " + arrInt[0] + ", is " + y1 + "$");
        System.out.println("Your Total for year: " + arrInt[1] + ", is " + y2 + "$");
        System.out.println("Your Total for year: " + arrInt[2] + ", is " + y3 + "$");
    } else if (yearsInvested <= 4) {
        System.out.println("Your Total for year: " + arrInt[0] + ", is " + y1 + "$");
        System.out.println("Your Total for year: " + arrInt[1] + ", is " + y2 + "$");
        System.out.println("Your Total for year: " + arrInt[2] + ", is " + y3 + "$");
        System.out.println("Your Total for year: " + arrInt[3] + ", is " + y3 + "$");
    } else if (yearsInvested <= 5) {
        System.out.println("Your Total for year: " + arrInt[0] + ", is " + y1 + "$");
        System.out.println("Your Total for year: " + arrInt[1] + ", is " + y2 + "$");
        System.out.println("Your Total for year: " + arrInt[2] + ", is " + y3 + "$");
        System.out.println("Your Total for year: " + arrInt[3] + ", is " + y3 + "$");
    }

 }

}
3
  • 2
    If the program works but you just want to optimize, perhaps the Code Review exchange is the best place to post this question? Commented Oct 24, 2017 at 2:13
  • Although, it doesn't seem like it's correct anyway, so maybe here is the best place after all. Commented Oct 24, 2017 at 2:16
  • thank you will check Code Review Exchange Commented Oct 24, 2017 at 2:23

2 Answers 2

1

You don't want explicit cases for each possible value of yearsInvested. Such an approach means you're probably copy-pasting code with slight modifications, which is prone to mistakes - and indeed, I see copy-paste mistakes; your 5 year case only goes to 4 years, and y3 is listed twice in the 4 and 5 year cases. (And imagine doing that for 241 years to try to calculate how much money would currently be in George Washington's bank account if he had left it alone since 1776 - that would be an unreasonable amount of code.)

Right now, the contents of your array are simply {1,2,3,4,5}. You certainly don't need an array to count to five. I don't believe an array is necessary for this program (you could probably just do both your input and output in a single for loop), but if you wanted to use one, it might make sense to put the results of your calculations in it.

You can use a for loop to put things into and read values out of the array - for example, reading the values out of the array might look like this:

for (int i = 0; i < yearsInvested; x++)
{
    System.out.println("Your Total for year: " + (i+1) + ", is " + resultsArray[i] + "$");
}
Sign up to request clarification or add additional context in comments.

Comments

0

You don't really need to store the totals at all - if all you are doing is printing out the cumulative total for each year then you can calculate as you go:

double investment = scanner.nextDouble();
...

for (int year = 0; year < yearsInvested; year++) {
    investment += investment * interestRate;
    System.out.println("Total year " + year + " is " + investment);
}

If you really need to store the values then I suggest you use a List rather than an array. They are much more straightforward for beginners to learn.

List<Integer> totals = new ArrayList<>();
for (int year = 0; year < yearsInvested; year++) {
    investment += investment * interestRate;
    totals.add(investment);
}

1 Comment

thank you appreciate the help, was able to cut it down from from 85 lines just to 25...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.