Now, in the email field, the **user** may put a single backslash (if they were experimenting or just by mistake) and this would crash the program. Please note that the **user** does not know that they have to escape a backslash in Java, ...
Nope. The user is not entering the string "in java" they are entering the string in an HTML form. It gets encoded by the browser and sent with the http request. It is read from the input stream and converted into a Java string by the JSP/Servlet container.
The string will be constructed before you ever get it.
The problem that you are having is that you are trying to construct a string constant and javac can't deal with it. "\l" can't be converted into a java class.
Unless you are asking your user to type in a java program for you to compile you don't have a problem with the backslash.
... So in Java is there a way of throwing an exception if a string is not constructed properly i.e. it contains an improperly escaped character??
If that is the case, your method will never get the string. If some other part of the program somehow has trouble creating the string, they will have to deal with it. Some sort of exception will be thrown in that part of the code and the String object will never be created. Your method accepts a String - if they don't have a String, they can't call the method. So there is nothing you have to do in your method to see if the string is a string - it already is.
In the make-believe code above, if there is some problem with the string creation, an excpetion will be thrown, and the EmailValidator is never invoked.
But I really don't think there is any problem. You are having a compile problem, not a problem with running code.
I had thought that throwing an IllegalArgumentException would do the trick but alas.
Exceptions are thrown and caught when the program is RUNNING. You are seeing the problem when you try to COMPILE.
If you write code that can't compile, it can't run and catching exceptions is meaningless.
Of course, I might be doing this the wrong way, I�m still learning. Somebody told me that I shouldn�t be doing this application in this manner i.e passing the email string into the constructor but they did not tell me how I should go about doing it instead. So any more thoughts? Please, anybody??
Your main method is just there to test the code. Make it work for that purpose.
Then, write your jsp page with the html form and see how that works. I'm pretty sure you will have no problems with backslashes.
\ \ \ \ \ \ <- Look, those backslashes there came from an html form
Make your jsp page do something simple with the entry, like System.out.println or something. Then once you like that, hook it up to your EmailValidator.