我在使用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
当我输入这个时,我意识到我只有一堆未声明的变量。这是对的吗?
答案 0 :(得分:2)
您正在其他方法 validEmail
中实施方法main
,这是不允许的。将您的方法移出main函数(在最后一行之前(在关闭大括号}
之前))。 您还在方法实现中的两个位置使用了额外的分号(第一行结束和最后一行结束)。删除分号;
。最后在方法签名中添加参数类型类(String)。更新后的方法应如下所示:
private static boolean validEmail(String 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]");
}
答案 1 :(得分:1)
您不能内联声明函数。将validEmail()
的声明移到main()
之外。也应该从方法声明的末尾删除分号。