简体   繁体   English

我不知道怎么了,我的逻辑似乎正确

[英]I can't figure out what's wrong, my logic seems correct

I know that what's messing up is my checkIfEdu method. 我知道搞砸的是我的checkIfEdu方法。 Everything else is fine because I've tried it without that method. 其他一切都很好,因为我尝试了没有该方法的情况。 However, it's reading all the data into the InvalidStudentEmails file and its because of that method! 但是,由于这种方法,它会将所有数据读取到InvalidStudentEmails文件中! i know it, everything else if fine because when I take that method out, it does everything correctly. 我知道,其他一切都很好,因为当我采取该方法时,它会正确执行所有操作。 Please help!!! 请帮忙!!!

import java.util.Scanner;
public class CheckStudentEmail
{
    public static void main (String[] args) throws Exception
    {
        java.io.File filein = new java.io.File("StudentEmailData.txt");
        java.io.File fileout1 = new java.io.File("ValidStudentEmails.txt");
        java.io.File fileout2 = new java.io.File("InvalidStudentEmails.txt");
        Scanner input = new Scanner(filein);
        java.io.PrintWriter validOutput = new java.io.PrintWriter(fileout1);
        java.io.PrintWriter invalidOutput = new java.io.PrintWriter(fileout2);

        writeHeadings(validOutput, invalidOutput);
        StudentEmail student = new StudentEmail();
        while (input.hasNext())
        {
                student.readStudentData(input);
                if (student.checkPeriod() == true && student.checkAtSymbol() == true
                   && student.checkIfEdu() == true)
                    student.writeEmails(validOutput);
                else
                    student.writeEmails(invalidOutput);
        }
        input.close();
        validOutput.close();
        invalidOutput.close();
    }

    public static void writeHeadings(java.io.PrintWriter validOutput,  java.io.PrintWriter invalidOutput)
    {
        validOutput.printf("%-20s%20s", "Name", "Email Address (Valid)");           validOutput.println();          validOutput.println();
            invalidOutput.printf("%-20s%20s", "Name", "Email Address (Invalid)");
            invalidOutput.println();
            invalidOutput.println();
        }

}

Here are my methods 这是我的方法

import java.util.Scanner;
public class StudentEmail
{
    private String stuName;
    private String stuEmail;

    StudentEmail()
    {}

    StudentEmail(String name, String email)
    {
        stuName = name;
        stuEmail = email;
    }

    public void readStudentData(Scanner input)
    {
        stuName = input.next();
        stuEmail = input.next();
    }

    public boolean checkPeriod()
    {
        if (stuEmail.indexOf(".") != -1)
            return true;
        else
            return false;
    }

    public boolean checkAtSymbol()
    {
        int atSymbol;
        if (stuEmail.indexOf('@') != -1)
        {
            atSymbol = stuEmail.indexOf('@');
            if (stuEmail.indexOf('@', atSymbol+1) != -1)
                return false;
            else
                return true;
        }
        else
            return false;
    }

    public boolean checkIfEdu()
    {
        int lengthOfEmail = stuEmail.length();
        int position = lengthOfEmail - 3;
        String checkWord = stuEmail.substring(position);
        if (checkWord == "edu")
            return true;
        else
            return false;
    }

    public void writeEmails(java.io.PrintWriter output)
    {
        output.printf("%-20s%20s", stuName, stuEmail);
        output.println();
    }

}

you compare strings with the equals() method. 您将字符串与equals()方法进行比较。 Using '==' is comparing object references 使用'=='比较对象引用

Did you check what's the checkIfEdu() return ? 您是否检查过checkIfEdu() return

Another thing is, as I have tried to run your checkIfEdu() it always return false. 另一件事是,正如我尝试运行您的checkIfEdu()它总是返回false。 Because you are comparing it as reference. 因为您正在比较它作为参考。

To compare String, this should be like this: 要比较String,应如下所示:

if (checkWord.equals("edu")) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM