Results 1 to 14 of 14

Thread: Homework help

  1. #1
    noobster is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by noobster View Post
    ... ( 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
    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! :)

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Hmmm too slow :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Hmmm too slow :)
    .............. :)

  6. #6
    gmarut is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    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. :)

  7. #7
    noobster is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    thanks guys for the help I really appreciate it. yea, the instructions says I do have to write a loop.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by noobster View Post
    thanks guys for the help I really appreciate it. yea, the instructions says I do have to write a loop.
    OK, but were you able to solve your problem with the advice given above?

  9. #9
    noobster is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Hmmm too slow :)
    I'm sorry I'm afraid I don't understand this ( but I feel it might be a joke at my expense :confused: )
    ----
    as for the helping I'm writing my program later this afternoon and will post it tonight if I get the chance to.

  10. #10
    gmarut is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by noobster View Post
    I'm sorry I'm afraid I don't understand this ( but I feel it might be a joke at my expense :confused: )
    doubt it, looks like its because Fubarable posted a response seconds before al_Marshy_1981 did

  11. #11
    noobster is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Hmmm too slow :)
    Quote Originally Posted by gmarut View Post
    doubt it, looks like its because Fubarable posted a response seconds before al_Marshy_1981 did
    guess your right
    :D

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Indeed, it was because Fubarable posted just before me

  13. #13
    noobster is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    hey guys here it is::: with problems :(. I keep getting an else without if error. any help?
    --------------------------------

    Java 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 " );
      	}
      
    
     	}
     }
    Moderator Edit: code tags added
    Last edited by Fubarable; 10-31-2010 at 08:59 PM. Reason: Moderator Edit: code tags added

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by noobster View Post
    hey guys here it is::: with problems :(. I keep getting an else without if error. any help?
    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:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck!

Similar Threads

  1. Homework Help
    By Kyoto in forum New To Java
    Replies: 2
    Last Post: 07-30-2010, 12:30 AM
  2. Homework help please
    By chick in forum New To Java
    Replies: 22
    Last Post: 03-19-2010, 07:39 AM
  3. Homework help
    By rclausing in forum New To Java
    Replies: 26
    Last Post: 11-24-2009, 06:06 AM
  4. help with homework
    By pinkdiamondgail in forum Advanced Java
    Replies: 7
    Last Post: 04-07-2009, 01:34 AM
  5. Homework help...
    By robrobrob in forum New To Java
    Replies: 4
    Last Post: 10-17-2008, 04:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •