substring problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

substring problem

 
0
  #1
Oct 14th, 2005
hi, I'm a first-level java learner, so my questions might seem really basic. but I don't really have a clue as to what I'm doing wrong. Can you guys please help out?

I'm supposed to write a program that ask the user to input their email address. and The first letter and the last letter cannot be '@'. Moreover, '@' is supposed to be included somewhere in the middle of the input.
I'm asking the user to input the data in PersonDB class. and I'm trying to write the validator for the email in another class called the Validator.

so in the PersonDB class, I have:

  1.  
  2. System.out.println("Enter Email Address: ");
  3. String email = sc.next();
  4. int strLength = email.length();
  5. System.out.println("" + strLength);
  6. c.setEmail(email);

and in the Validator class, I have:

  1.  
  2. boolean validateEmail(String emailValid)
  3. {
  4. isvalid = false;
  5. while (emailValid[0] =='@' || emailValid[emailValid - 1] == '@')
  6. {
  7. System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.\n");
  8. }
  9.  
  10. if (emailValid[0] != '@' && emailValid[strLength - 1] != '@')
  11. {
  12. if (int index = 0, index < strLength, index++)
  13. {
  14. //don't know what to do here
  15. }
  16. }
  17.  
  18. return isvalid;
  19. }

I know this is not right. But I don't have a clue as to what I should do.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: substring problem

 
0
  #2
Oct 14th, 2005
Ok, I think I see what you're having trouble with....You don't know what to do when it pasts the first test(first and last character are not @), but you're not sure how to check if it's somewhere in the middle....As soon as you find one you could return true:
  1. if (int index = 1, index < strLength-1, index++)
  2. {
  3. if (emailValid[index].equals("@"))
  4. {
  5. return true;
  6. }
  7. else
  8. {
  9. if (index == strLength-2)
  10. {
  11. return false;
  12. }
  13. }
  14. }


The second problem you have is using ==. This might work, but I'm not sure that it will. Since you are comparing string values(not string objects) you need to use the .equals() method.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: substring problem

 
0
  #3
Oct 14th, 2005
Hi,
Following your advices, I've made the changes to my program accordingly. But I still have errors that I don't know how to fix.

PersondDB code:

  1. //String email_String = Validator.getString(sc, "Enter Email Address: ");
  2. System.out.println("Enter Email Address: ");
  3. String email = sc.next();
  4. int strLength = email.length();
  5. System.out.println("" + strLength);
  6. Validator.validateEmail(email, strLength);
  7. c.setEmail(email);

Validator code:

  1.  
  2. public boolean validateEmail(String emailValid, int strLength)
  3. {
  4. isvalid = false;
  5.  
  6. while ((emailValid[0].equals('@'))
  7. || (emailValid[emailValid - 1].equals('@')))
  8. {
  9. System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.\n");
  10. }
  11.  
  12. if (int index = 1, index < strLength-1, index++)
  13. {
  14. if (emailValid[index].equals("@"))
  15. {
  16. return true;
  17. }
  18. else
  19. {
  20. if (index == strLength-2)
  21. {
  22. return false;
  23. }
  24. }
  25. }
  26.  
  27. return isvalid;
  28. }

And this is the list of errors I have:

.\Validator.java:23: '.class' expected
if (int index = 1, index < strLength-1, index++)
^
.\Validator.java:38: ')' expected
return isvalid;
^
X:\java CS170\Lab 2b\PersonDB.java:33: non-static method validateEmail(java.lang.String,int) cannot be referenced from a static context
Validator.validateEmail(email, strLength);
^
.\Validator.java:15: cannot find symbol
symbol : variable isvalid
location: class Validator
isvalid = false;
^
.\Validator.java:17: array required, but java.lang.String found
while ((emailValid[0].equals('@'))
^
.\Validator.java:18: operator - cannot be applied to java.lang.String,int
|| (emailValid[emailValid - 1].equals('@')))
^
.\Validator.java:18: array required, but java.lang.String found
|| (emailValid[emailValid - 1].equals('@')))
^
.\Validator.java:23: unexpected type
required: value
found : class
if (int index = 1, index < strLength-1, index++)
^
8 errors
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 71
Reputation: Kate Albany is an unknown quantity at this point 
Solved Threads: 1
Kate Albany Kate Albany is offline Offline
Junior Poster in Training

Re: substring problem

 
0
  #4
Oct 15th, 2005
Hi,

There are still several problems in the code, please see comments below:

  1. // You do not need to pass in the length, a String object already knows its own length
  2. // Also you appear to be 'stuck' in a static context (in 'main' I expect), you will need to make this method static or create an Object instance in 'main'
  3. public boolean validateEmail(String emailValid, int strLength)
  4. {
  5. // This is not declared and also never altered, why not just 'return false' at the end
  6. isvalid = false;
  7.  
  8. // This should not be a 'while' loop as it is a one off check, use an 'if' instead
  9. // emailValid - 1 should be emailValid.length() - 1
  10. while ((emailValid[0].equals('@')) || (emailValid[emailValid - 1].equals('@'))) {
  11. System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.\n");
  12. }
  13.  
  14. // This should be a 'for' loop not an 'if' statement
  15. // You should really brush up on your control statements. I.E. if, while, for etc ...
  16. if (int index = 1, index < strLength-1, index++) {
  17. if (emailValid[index].equals("@")) {
  18. return true;
  19. }
  20. // Why not just loop until strLength-2
  21. else {
  22. if (index == strLength-2) {
  23. return false;
  24. }
  25. }
  26. }
  27.  
  28. // Just return false instead
  29. return isvalid;
  30. }
A modified version is below:

  1. public boolean validateEmail (String asAddress)
  2. {
  3. if (asAddress == null) { return false; }
  4.  
  5. if ((asAddress[0].equals('@')) || (asAddress[(asAddress.length()-1)].equals('@'))) {
  6. System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.");
  7. }
  8.  
  9. for (int index = 1, index < (asAddress.length()-2), index++) {
  10. if (asAddress[index].equals("@")) {
  11. return true;
  12. }
  13. }
  14.  
  15. return false;
  16. }
Or better yet would be to use the String Object's own methods.

  1. public boolean validateEmail (String asAddress)
  2. {
  3. if (asAddress == null) {
  4. System.out.println("Error: Email Address: Is null");
  5. return false;
  6. }
  7.  
  8. if (asAddress.startsWith('@')) {
  9. System.out.println("Error: Email Address: Starts with '@'");
  10. return false;
  11. }
  12.  
  13. if (asAddress.endsWith('@')) {
  14. System.out.println("Error: Email Address: Ends with '@'");
  15. return false;
  16. }
  17.  
  18. if (asAddress.indexOf('@') == -1) {
  19. System.out.println("Error: Email Address: Must contain '@'");
  20. return false;
  21. }
  22.  
  23. return true;
  24. }

I am not usually in the habit of giving complete answers as code, and will likly get flamed for doing so (I was bored). So please research the methods used above from the URL below or you will learn nothing!!, which would be partly my fault.

http://java.sun.com/j2se/1.5.0/docs/...ng/String.html

Also as I mentioned in the code comments, you really need to get to grips with ... if, for, while etc ...

Kate
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: substring problem

 
0
  #5
Oct 15th, 2005
Better yet....

Use the JavaMail InternetAddress class.

Regards,

Nate

PS: if you're doing this for work you need to read the RFC # 822 for all the rules to a properly formated email address.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: substring problem

 
0
  #6
Oct 15th, 2005
hi hooknc,
Thanks for suggestion. I didn't know about the endsWith and the startsWith methods. Please don't feel guilty. I'm a visual learner. I learn by example. That's why I sometimes doubt programming is the right thing for me. your code helps me a lot. I read the articles...don't really understand everything, but I know that all those methods exist now.

oh I also want to point out that:
if (asAddress.startsWith('@')).....

should use the double quote around @, like this if (asAddress.startsWith("@"));

else the compiler throws errors.

I'm sure you know that already. I'm pointing it out for anyone else who might be having the same problem and reviewing this thread.

I don't understanding one thing though:

in the following code

  1.  
  2. if (asAddress.indexOf("@") == -1) {
  3. System.out.println("Error: Email Address: Must contain '@'");
  4. return false;
  5. }

why is it -1?

Thanks again,
Karen
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: substring problem

 
0
  #7
Oct 15th, 2005
The -1 means that the string that you're searching for isn't found inside the string being searched.

Regards,

Nate
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC