gpt4 book ai didi

java - Java 电子邮件验证中的编译器错误

转载 作者:行者123 更新时间:2023-12-02 00:05:51 26 4
gpt4 key购买 nike

我在 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,这是不允许的。将下面的方法移出主函数(就在最后一行之前(在类右大括号}之前))。 此外,您还在方法实现中的两个位置(第一行结束和最后一行结束)使用了额外的分号。删除分号 ;。最后在方法签名中添加参数类型类(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]");
}

关于java - Java 电子邮件验证中的编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13899370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com