Results 1 to 14 of 14
Thread: Homework help
- 10-26-2010, 09:09 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 8
- Rep Power
- 0
Homework help
Hey everyone. I have a quickie ( probably easy question for most ) to ask. I have to write a program ( which i'm currently writing ) that reads user input ( I've imported the Scanner class for this ) of an email address. I'm suppose to run a loop that checks each letter one by one for an @ character. If there is only one @ character. I print out that it is a valid email otherwise print out that the email is invalid. My question is I would a do/while loop work best for this? ( considering I won't know the span of the email address) and if so how would I deal with the @ character ( since I know its not considered an int or something ) Thanks in advance for the help
Last edited by noobster; 10-26-2010 at 09:11 PM. Reason: Fragment sentance O.o
-
You will know the length of the String though since String has a length() method that returns this int. A general rule that you probably already know is, any time you know in advance how many times a loop will iterate, use a for loop, and so I recommend a for loop.
Best of luck with your assignment and your Java studies! :)
- 10-26-2010, 09:18 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
I would use a for loop checking charAt() String method through each iteration for the @ symbol. A counter variable to count how many times the @ symbol appears.
- 10-26-2010, 09:19 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Hmmm too slow :)
-
- 10-26-2010, 09:28 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Do you have to use a loop? Impress your teacher by doing RegEx email validation... or he may not accept it since he probably explictly said to do it in a loop. :)
- 10-27-2010, 03:34 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 8
- Rep Power
- 0
thanks guys for the help I really appreciate it. yea, the instructions says I do have to write a loop.
-
- 10-27-2010, 03:13 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 8
- Rep Power
- 0
- 10-27-2010, 03:24 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
- 10-27-2010, 03:26 PM #11
Member
- Join Date
- Sep 2010
- Posts
- 8
- Rep Power
- 0
- 10-27-2010, 03:33 PM #12
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Indeed, it was because Fubarable posted just before me
- 10-31-2010, 07:45 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 8
- Rep Power
- 0
hey guys here it is::: with problems :(. I keep getting an else without if error. any help?
--------------------------------
Moderator Edit: code tags addedJava Code:/* * Chapter 6 Question 62 */ import java.util.Scanner; public class Chapter6Question62 { public static void main( String [] args ) { //char a = ' @ '; Scanner scan = new Scanner ( System.in ); System.out.print( " Please enter your email address " ); String email1 = scan.next(); int count = 0; for ( count = 0; count >= 20; count ++ ) if ( email1.charAt('@') == '@') count ++; //( count >= 1) { System.out.println( " That email address was vaild " ); }else{ System.out.println(" That email address isn't vaild " ); } } }Last edited by Fubarable; 10-31-2010 at 08:59 PM. Reason: Moderator Edit: code tags added
-
Indeed your else is not on the same level as the if, so there's no surprise that you're getting error messages. You'll want to enclose all if blocks, else blocks, for loops, while loops, shoot any blocks or loops inside of curly braces and you'll want to format your code well. Do that and these types of errors will become obvious to you.
Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right? I've added tags to your code above.
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luck!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
Similar Threads
-
Homework Help
By Kyoto in forum New To JavaReplies: 2Last Post: 07-30-2010, 12:30 AM -
Homework help please
By chick in forum New To JavaReplies: 22Last Post: 03-19-2010, 07:39 AM -
Homework help
By rclausing in forum New To JavaReplies: 26Last Post: 11-24-2009, 06:06 AM -
help with homework
By pinkdiamondgail in forum Advanced JavaReplies: 7Last Post: 04-07-2009, 01:34 AM -
Homework help...
By robrobrob in forum New To JavaReplies: 4Last Post: 10-17-2008, 04:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks