Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Having trouble understanding an error code i keep getting. Please help!

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 deposit is 0.0
Your total withdrawls were 0.0
Your total interest is 0.008333333333333333
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

[fbr - added code tags]

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: 12122
30
I added code tags to make your post slightly more readable.