943,832 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2024
  • Java RSS
Oct 14th, 2005
0

substring problem

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Oct 14th, 2005
0

Re: substring problem

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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Oct 14th, 2005
0

Re: substring problem

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:

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Oct 15th, 2005
0

Re: substring problem

Hi,

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

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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.

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Kate Albany is offline Offline
71 posts
since Jun 2005
Oct 15th, 2005
0

Re: substring problem

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.
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Oct 15th, 2005
0

Re: substring problem

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

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Oct 15th, 2005
0

Re: substring problem

The -1 means that the string that you're searching for isn't found inside the string being searched.

Regards,

Nate
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: image via java
Next Thread in Java Forum Timeline: need help asap :)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC