This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center。
已关闭8年。
我在JAVA中进行电子邮件验证时遇到了麻烦(这是新问题),不断收到这些错误,并且对原因感到困惑。我应该收到有关人员的电子邮件,检查它是否符合我的教授设定的标准,然后获取有关测试的信息。当我检查电子邮件时,如果出现错误,则应该显示错误。
失误
码
*编辑
好了,现在我有了这段代码
我现在正在得到这些
当我键入此内容时,我意识到我只有一堆未声明的变量。正确吗?
已关闭8年。
我在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
当我键入此内容时,我意识到我只有一堆未声明的变量。正确吗?
最佳答案
您正在另一种不允许的方法 validEmail
内实现方法main
。将您的方法从主函数中移出(恰好在最后一行之前(类关闭大括号}
之前))。 另外,您在方法实现中的两个位置(第一行末尾)使用多余的分号。删除分号;
。最后,在方法签名中添加参数类型类(字符串)。更新后的方法应如下所示:
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]");
}