0

I'm a beginner to Java, and I'm trying to write a simple Java program that calculates an amount in a savings account based on an initial amount deposited, number of years, and an interest rate. This is my code, which compiles, but does not actually print the amount of money in the account (basically, it completely ignores my second method).

import java.util.*;

class BankAccount {

public static Scanner input = new Scanner(System.in); 
public static double dollars;
public static double years; 
public static double annualRate; 
public static double amountInAcc; 

public static void main(String[] args) {

    System.out.println("Enter the number of dollars."); 
    dollars = input.nextDouble(); 

    System.out.println("Enter the number of years."); 
    years = input.nextDouble(); 

    System.out.println("Enter the annual interest rate."); 
    annualRate= input.nextDouble(); 
    }


public static void getDouble() {
    amountInAcc = (dollars * Math.pow((1 + annualRate), years)); 
    System.out.println("The amount of money in the account is " + amountInAcc); 
    } 
}

I think it is because I don't call the method anywhere, but I'm a bit confused as to how/where I would do that.

1
  • "I think it is because I don't call the method anywhere" well you are right "but I'm a bit confused as to how/where I would do that" well, in your case best place would be after you read all values from user, so maybe invoke BankAccount.getDouble(); and print its result right before end of main method. Commented Aug 4, 2014 at 0:15

2 Answers 2

1

Call it once you have received all the required inputs from the Scanner.

// In main
System.out.println("Enter the number of dollars."); 
dollars = input.nextDouble(); 

System.out.println("Enter the number of years."); 
years = input.nextDouble(); 

System.out.println("Enter the annual interest rate."); 
annualRate= input.nextDouble(); 

getDouble(); // Print out the account amount. 
Sign up to request clarification or add additional context in comments.

Comments

0

The main method is pretty much the entry point for the program to run. To call a static method in java you can just go:

public static void main(String[] args) {
...
    BankAccount.getDouble();
...
}

If it wasn't static you have to create an instance of the class. like:

BankAccount account = new BankAccount();
account.getDouble();

Comments

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.