| | |
substring problem
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Solved Threads: 0
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:
and in the Validator class, I have:
I know this is not right. But I don't have a clue as to what I should do.
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)
System.out.println("Enter Email Address: "); String email = sc.next(); int strLength = email.length(); System.out.println("" + strLength); c.setEmail(email);
and in the Validator class, I have:
Java Syntax (Toggle Plain Text)
boolean validateEmail(String emailValid) { isvalid = false; while (emailValid[0] =='@' || emailValid[emailValid - 1] == '@') { System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.\n"); } if (emailValid[0] != '@' && emailValid[strLength - 1] != '@') { if (int index = 0, index < strLength, index++) { //don't know what to do here } } return isvalid; }
I know this is not right. But I don't have a clue as to what I should do.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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:
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.
Java Syntax (Toggle Plain Text)
if (int index = 1, index < strLength-1, index++) { if (emailValid[index].equals("@")) { return true; } else { if (index == strLength-2) { return false; } } }
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.
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Solved Threads: 0
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:
Validator code:
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
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)
//String email_String = Validator.getString(sc, "Enter Email Address: "); System.out.println("Enter Email Address: "); String email = sc.next(); int strLength = email.length(); System.out.println("" + strLength); Validator.validateEmail(email, strLength); c.setEmail(email);
Validator code:
Java Syntax (Toggle Plain Text)
public boolean validateEmail(String emailValid, int strLength) { isvalid = false; while ((emailValid[0].equals('@')) || (emailValid[emailValid - 1].equals('@'))) { System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.\n"); } if (int index = 1, index < strLength-1, index++) { if (emailValid[index].equals("@")) { return true; } else { if (index == strLength-2) { return false; } } } return isvalid; }
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
•
•
Join Date: Jun 2005
Posts: 71
Reputation:
Solved Threads: 1
Hi,
There are still several problems in the code, please see comments below:
A modified version is below:
Or better yet would be to use the String Object's own methods.
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
There are still several problems in the code, please see comments below:
Java Syntax (Toggle Plain Text)
// You do not need to pass in the length, a String object already knows its own length // 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' public boolean validateEmail(String emailValid, int strLength) { // This is not declared and also never altered, why not just 'return false' at the end isvalid = false; // This should not be a 'while' loop as it is a one off check, use an 'if' instead // emailValid - 1 should be emailValid.length() - 1 while ((emailValid[0].equals('@')) || (emailValid[emailValid - 1].equals('@'))) { System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.\n"); } // This should be a 'for' loop not an 'if' statement // You should really brush up on your control statements. I.E. if, while, for etc ... if (int index = 1, index < strLength-1, index++) { if (emailValid[index].equals("@")) { return true; } // Why not just loop until strLength-2 else { if (index == strLength-2) { return false; } } } // Just return false instead return isvalid; }
Java Syntax (Toggle Plain Text)
public boolean validateEmail (String asAddress) { if (asAddress == null) { return false; } if ((asAddress[0].equals('@')) || (asAddress[(asAddress.length()-1)].equals('@'))) { System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address."); } for (int index = 1, index < (asAddress.length()-2), index++) { if (asAddress[index].equals("@")) { return true; } } return false; }
Java Syntax (Toggle Plain Text)
public boolean validateEmail (String asAddress) { if (asAddress == null) { System.out.println("Error: Email Address: Is null"); return false; } if (asAddress.startsWith('@')) { System.out.println("Error: Email Address: Starts with '@'"); return false; } if (asAddress.endsWith('@')) { System.out.println("Error: Email Address: Ends with '@'"); return false; } if (asAddress.indexOf('@') == -1) { System.out.println("Error: Email Address: Must contain '@'"); return false; } return true; }
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
•
•
Join Date: Aug 2005
Posts: 216
Reputation:
Solved Threads: 8
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.
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.
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Solved Threads: 0
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
why is it -1?
Thanks again,
Karen
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)
if (asAddress.indexOf("@") == -1) { System.out.println("Error: Email Address: Must contain '@'"); return false; }
why is it -1?
Thanks again,
Karen
![]() |
Similar Threads
- Problem In using Substring.. (Java)
Other Threads in the Java Forum
- Previous Thread: image via java
- Next Thread: need help asap :)
| Thread Tools | Search this Thread |
3d @param affinetransform android api applet application arc arguments array arrays automation binary bluetooth byte capture chat class classes click client code color compare component count database design detection eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer j2me java java.xls javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong print problem producer program programming project projectideas read recursion replaysolutions rim scanner screen server set size sms sort sql string swing terminal threads time transforms tree ui web windows






