Originally posted by eric elysia:
I am trying to write code that makes sure the user does enter a period.
Yes, but do you see what happens if the user enters a String without a period? Let's see what happens when you enter "hi" without the quotes.
Since 'h' is not a period, the loop body is executed, placing 'h' into the array a.
Since 'i' is not a period, the loop body is executed, placing 'i' into the array a.
Since i is now outside the bounds of legal values for the String, you get a StringIndexOutOfBoundsException.
I recommend thoroughly reading the java.lang.String JavaDocs as there are several methods in that class that would be very helpful here. Since String is such a core class, it's a very good idea to become familiar with all of its methods.
For example, you want to remove all spaces from the String and test to see if it ends in a period. trim() will remove leading and trailing whitespace (not spaces between words), and endsWith() will tell you if one String ends with another String. Or you could use charString.charAt(charString.length()-1) to check the last char of the String.
Also, if you're using JDK 1.4 or later and you're allowed to use the regular expression methods, you could remove all non-letters with a single line of code using replaceAll().
[ Grrrrrrammar is goooooood! ]
[ April 26, 2005: Message edited by: David Harkness ]