我在 JAVA 中的电子邮件验证遇到问题(对此新手)不断收到这些错误,并且对原因感到困惑。我应该收到这些人的电子邮件,检查它的设定标准(由我的教授),然后获取有关测试的信息。当我检查电子邮件时,如果有错误,我应该显示错误。
错误
N:\Programming\Java\Homework\EmailAndGrade.java:29: illegal start of expression
private static boolean validEmail(sEmail);
^
N:\Programming\Java\Homework\EmailAndGrade.java:29: illegal start of expression
private static boolean validEmail(sEmail);
^
N:\Programming\Java\Homework\EmailAndGrade.java:29: ';' expected
private static boolean validEmail(sEmail);
^
3 errors
代码
public class EmailAndGrade
{
public static void main(String[] args)
{
//Variables
String sEmail, sError, sTest;
int iTest;
char cGrade;
sEmail = JOptionPane.showInputDialog("Enter your email: ");
private static boolean validEmail(sEmail);
{
// editing to make requirements listed
// return email.matches("[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}");
return email.matches("[A-Z0-9]{7}+@[springfieldcollege]+\\.[edu]");
};
sError = "One or More Errors with the Input Email.\n" +
"Email is Empty.\n" +
"Email must be less than 30 characters.\n" +
"Must end in: @springfieldcollege.edu.\n" +
"Can only have one @.\n" +
"Must start with A through Z or a through z.\n" +
"Can not have: # $ % & ,\n ";
sTest = JOptionPane.showInptDialog("Enter Test Grade: ");
iTest = Integer.parseInt(sTest);
System.exit(0);
}
/*
void checkEmails()
{
for(String email : emailAddresses) {
if(validEmail(email)) {
// it's a good email - do something good with it
}
else {
// it's a bad email - do something... bad to it? sounds dirty...
JOptionPane.showMessageDialog(null,
sError,
"Email Error - Killian O'Brien",
JOptionPane.INFORMATION_MESSAGE);
}
}*/
}
*编辑
好的,现在我有这个代码
package com.mkyong.regex;
import javax.swing.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailAndGrade
{
public static void main(String[] args)
{
//Variables
String sEmail, sError, sTest;
int iTest;
char cGrade;
sEmail = JOptionPane.showInputDialog("Enter your email: ");
sError = "One or More Errors with the Input Email.\n" +
"Email is Empty.\n" +
"Email must be less than 30 characters.\n" +
"Must end in: @springfieldcollege.edu.\n" +
"Can only have one @.\n" +
"Must start with A through Z or a through z.\n" +
"Can not have: # $ % & ,\n ";
sTest = JOptionPane.showInptDialog("Enter Test Grade: ");
iTest = Integer.parseInt(sTest);
System.exit(0);
}
public static boolean validEmail(String sEmail){
return email.matches("[A-Z0-9]{7}+@[springfieldcollege]+\\.[edu]");
}
public static void checkEmails(){
for(String sEemail : emailAddresses) {
if(validEmail(email)) {
// it's a good email - do something good with it
}
else {
// it's a bad email - do something... bad to it? sounds dirty...
JOptionPane.showMessageDialog(null,
sError,
"Email Error - Killian O'Brien",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
我现在得到这些
N:\Programming\Java\Homework\EmailAndGrade.java:37: cannot find symbol
symbol : method showInptDialog(java.lang.String)
location: class javax.swing.JOptionPane
sTest = JOptionPane.showInptDialog("Enter Test Grade: ");
^
N:\Programming\Java\Homework\EmailAndGrade.java:47: cannot find symbol
symbol : variable emailAddresses
location: class com.mkyong.regex.EmailAndGrade
for(String email : emailAddresses) {
^
N:\Programming\Java\Homework\EmailAndGrade.java:54: cannot find symbol
symbol : variable sError
location: class com.mkyong.regex.EmailAndGrade
sError,
^
N:\Programming\Java\Homework\EmailAndGrade.java:63: cannot find symbol
symbol : variable email
location: class com.mkyong.regex.EmailAndGrade
return email.matches("[A-Z0-9]{7}+@[springfieldcollege]+\\.[edu]");
^
4 errors
当我输入这个时,我意识到我只有一堆未声明的变量。那会是正确的吗?