Your job for this assignment is to create a simple program thatdetermines if a s
ID: 3609177 • Letter: Y
Question
Your job for this assignment is to create a simple program thatdetermines if a string is an email address. Roughly speaking, anemail address is a string that contains exactly one occurrence ofthe symbol "@", does not start with a ".", and has certain otherrestrictions on the kinds of symbols allowed. For a fairly completeaccount of those restrictions, read: http://en.wikipedia.org/wiki/E-mail_address. To simply mattershowever, we'll assume our own slightly simplified rules: a stringis a legal email address if:* it does not begin with a "." or an "@";
* has exactly one occurence of @;
* it is made up only of numbers, letters, dots, or @.
Your job for this assignment is to write a single class applicationcalled
EMail (file name:EMail.java),which reads in a single string from the keyboard; if the string isa legal address according to this definition, your program shouldprint
Legal email address? trueif the string isn't legal, your program should print
Legal email address? falseSome tips:
1) You may want to use a (static) method in the Character classcalled isLetterOrDigit, which returns boolean. Thus
Character.isLetterOrDigit('a')returns
true, butCharacter.isLetterOrDigit('@')returns
false.This is what i have so far, and somehow whenever i entered an emaillike "bebe@yahoo.com" it gives me "it is valid" and "it isinvalid". Can someone help me how to fix this problem? thanks.
import java.util.Scanner;
public class Email {
public static void main(String[] args) {
System.out.println("Enter the email: ");
Scanner scan = new Scanner(System.in);
String email = scan.nextLine();
System.out.println(email);
int count = 0;
for( int i = 0; i<email.length(); i++){
char letter = email.charAt(i);
if(letter == '@'){
{
count ++;
}
if(email.charAt(0) =='@' ||email.charAt(0) == '.' ||count !=1)
System.out.println("It isInvalid");
//else if(count !=1)
// System.out.println("invalid");
else
System.out.println("It isValid");
}
}
}
}
Explanation / Answer
Dear User, below is the code that does what you need. The code might notcompile due to case errors as i do not have a java compiler handyat the moment. Please let me know if the comments are unclear or if you need moreclarification. public static void main(String[] args) { Systemout.println("enter the email: "); Scanner scan = new Scanner(System.in); String email.scan.nextLine(); System.out.println(email); System.out.println("Valid email? " +ValidateEmail(email)); } private void bool validateEmail(String email) { int count=0; //flag for number of '@' //if it start with @ or . stop checking andreturn false if(email.charAt90).Equals('@') ||email.charAt90).Equals('.')) { return False; } for (int i=0; i0) then return false there is two @in there if(count>0) { return false; } count++; } //if character is not letteror digit and it is not a '.' then return false invalid email //Must check for '.' since wealready checked it is not the start if(!(Character.isLetterOrDigit(part) &&part.equals('.'))) { returnfalse; } } //reached the end everythign is valid return true; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.