• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BackSlash in a String param

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I�m basically trying to write a class that validates an email address, checking the @, the dots etc. The class is available at www.ernieboy.com/EmailValidator.java This class will be used to validate email addresses entered by users in a web page.
Problem:
My constructor takes a String param as the only formal argument. If I supply a string which contains a backslash (the user could do this if they are experimenting) as the actual argument, then the class won�t compile because of the illegal escape character in the string. I have tried throwing an IllegalArgumentException but no, Java still complains. Please take a look at the code. Do you have any ideas??
Also, what do you think about my coding style? I�m still reading the books and getting to know the API.
Thanks in advance.
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your problem is here:
em = new EmailValidator( "hel\lo@world.com" );
If you want to create a java String literal containing a back-slash, you have to "escape" the back-slash:
em = new EmailValidator( "hel\\lo@world.com" );
Otherwise, the compiler tries to make sense out of the "\l" and can't figure it out.
 
ernest fakudze
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, as you can see from the source code, I�m passing a string literal to the constructor. Somebody has pointed out to me that if this was a real world situation then I would not capture the email address this way. Remember that I will be capturing it from a web page. So if doing it this way is not correct, what�s the better way of doing this??
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I see it, you have two testing issues here
That testing involves two sides - testing the method itself and testing the "data capture" (getting the data from the web page to the method).
I would probably start with the method itself - make sure it does what you want it to do.
You should test that it passes when it should, and fails when it should.
For example, you want it do something special with strings containing back-slashes. So you have to be able to build such a string in Java. The way you do that is using "\\" rather than "\". Or you could pass the string into your main method from the command line. Or read it from a file. But in each of these situations, you are constructing a string to see how the method reacts to that string. The issue at this point is not about how to make the strings, but if the method works.
Next, I'd take on the data capture tests. Somehow, you have to get the email address from somewhere into java. Each "user interface" you encounter will probably have its own strange issues. For example, HTTP doesn't really care about back-slashes, but other things like & vsa &amp; and < vs &lt; and SPACE vs %20, etc. So deal with these encoding issues as a separate set of tests. Make sure you can enter a string in a web form and get it to java in a "safe" way. At this stage don't worry about your email testing method, just make sure you know how to get the string.
Once both pieces work, you can put the two together. (and test agin the full thing).
Make sense?
 
ernest fakudze
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave,
I do understand what you mean. Let me try and clarify myself more if I can. I will have a JSP page which has a text input field to capture the email address from the user. In my JSP I will then instantiate my EmailValidator class in order to validate the email address. I have a method in my class which captures the email String. In my situation this method happens to be the constructor, it takes a string literal as an argument and then processes it.
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, but you and I do. I can�t capture the email string as an argument to a main method because we don�t have a main method here. 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?? I had thought that throwing an IllegalArgumentException would do the trick but alas.
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??
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic