Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

dannygirl sylvest
Greenhorn
Posts: 9
I'm doing an assignment where i have to Design a SavingsAccount class that stores a savings account's annual interest rate and balance. the class constructor should accept the amount of the savings account's starting balance. The class should also have methods for subtracting the amount of withdrawal, add the amount of deposit, and adding the amount of monthly interest to the balance. The monthly interest rate is the annual interest rate divided by 12.

45 minutes ago - 1 week left to answer.
I forgot to mention what my project consisted of: Design a SavingsAccount class that stores a savings account's annual interest rate and balance. the class constructor should accept the amount of the savings account's starting balance. The class should also have methods for subtracting the amount of withdrawal, add the amount of deposit, and adding the amount of monthly interest to the balance. The monthly interest rate is the annual interest rate divided by 12. To add the monthly interest to the balance, multiply the monthly interest rate by the balance, and add the result to the balance. ( this is compound interest ) Use DecimalFormat class to format output.

I'm new at this and i am still trying to grasp the concept of calling methods and the use of constructors. When i run my code this is what happens
Your total balance is \$ 1,000.01
Calculate the month 2 (Y/N)
Exception in thread "main" java.lang.StringIndexOutOfBoundsExceptioâ€¦ String index out of range: 0
at java.lang.String.charAt(String.java:686)
at assignment06.testAccount.main(testAccounâ€¦
Java Result: 1
BUILD SUCCESSFUL (total time: 11 seconds)

i'm still not sure exactly what is going wrong . Thanks for your input

Paul Beckett
Ranch Hand
Posts: 96
Welcome to the Java Ranch!
Please can you use UseCodeTags when posting code samples.

Paul Beckett
Ranch Hand
Posts: 96
• 1
first lets look at your exception stack trace.

...StringIndexOutOfBoundsException. This indicates that your String does not contain a character at the index you've asked for. For the String "abcd" which has a length of 4 characters but the maximum index you can ask for is 3 (Strings are zero indexed). Your stack trace says "String index out of range: 0". This indicates you've asked for index 0 of the String and it wasn't found so its an empty String (if it was null you would get a NullPointerException instead).

...String.charAt. This first line tells you exactly where the exception was thrown (line 686 of the String class in the chatAt method). I can't see anywhere in your code where the charAt method is called. Is your code listing complete?

...testAccount.main. This is a very useful line in your stack trace and tells you from where the String.charAt method was called. The stack trace will include the line number to help you but you haven't included the full stack trace in your post.

fred rosenberger
lowercase baba
Bartender
Posts: 12085
29