【问题标题】:Class arrays and sorting info类数组和排序信息
【发布时间】:2014-03-10 19:26:30
【问题描述】:

我应该用 Java 编写一个程序。我收到了一个包含

的文本文件

我是哈坎。我的电子邮件地址是 hakan@cs.uh.edu,你叫什么名字?嗨,我的名字是 Tarikul,我最喜欢的电子邮件地址是 tarikul2000@cs.uh.edu

我应该将 tarikul2000@uh.edu 和 hakan@cs.uh.edu 并根据他们是否有子域(“cs”部分还有其他可能性)来组织它们并将其存储到类中Email 和 UniversityEmail 类型的数组。然后我将使用 0-7 的用户输入,并根据输入打印出一组不同的信息 我已经创建了课程,但我不知道如何获取信息然后对其进行排序。 可能的子域是

1.艺术 2. 切 3.化学 4. 心电图 5.cs 6. 埃格 7. 政策

import java.io.* ;
import java.util.HashSet;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


public class Try {
    public static void main(String[] args) {         
        Email [] storage;// email is a class that was made to store the data
        storage = new Email [99];
        UniversityEmail[] save;
        save= new UniversityEmail[99];

        HashSet<String> hs = new HashSet<>();
        Scanner input= null;
        PrintWriter output= null;
        try {
            input= new Scanner("inputemails.txt");
            output= new PrintWriter("outputemails.txt");

            }

         catch (FileNotFoundException e) {
            System.out.print("File not found");
            System.exit(0);}
        String line = null;

        while(input.hasNextLine())
        {
        fillEmailsHashSet(line, hs);
        }




        input.close();
        output.close();
    }


public static void fillEmailsHashSet(String line,HashSet<String> container){
    Pattern p = Pattern.compile("([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4})");
    Matcher m = p.matcher(line);

    while(m.find()) {
        container.add(m.group(1));
    }

}}

这些是我收到的关于这个程序的实际说明

  1. 连接到外部输入文件。输入文件的名称将始终为 inputemails.txt,并且 因此请不要在程序中询问文件名。
  2. 检测文件中的电子邮件地址。
  3. 如果电子邮件没有子域,请为该电子邮件创建电子邮件对象。
  4. 如果电子邮件有子域,请为该电子邮件创建 UniversityEmail 对象。
  5. 请将所有电子邮件存储在同一个(一个)数组列表中。
  6. 将文件中的所有邮件复制到数组列表后,请询问用户是什么类型的邮件 包含在输出文件中。如果用户输入0,请填写没有子域的邮件 数组列表。请注意,输出文件必须以表示电子邮件数量的数字开头 在文件中。如果用户输入的数字介于 1-7 之间,请填写来自特定部门的所有电子邮件 在输出文件中。请注意,输出文件必须以数字开头,表示 文件中的电子邮件。用户只能输入 0 到 7 之间的一个整数

我决定也包括我的其他课程信息,第一个是电子邮件课程

public class Email {

    public String username;
    public String email1;
    public String extension;
    public Email()
    {
        //default construntor

    }

    public Email(String U, String Em , String Ex)
    {
        username =U;
        email1= Em;
        extension=Ex;

    }

}

这是 UniversityEmail 类的代码

public class UniversityEmail extends Email {

    public String department;
    public int code;
    public UniversityEmail()
    {//default constructor 
    }
    public UniversityEmail(String U, String Em , String Ex
            , String Dep,int C)
    {super( U, Em , Ex);
    department= Dep;
    code=C;

    }

}

【问题讨论】:

  • 我阅读了您的帖子两次,但实际上仍然不知道您的要求。你的问题到底是什么?
  • 我怎样才能组织不同的电子邮件部分并将其放置到正确的数组中。到目前为止,我会在文本中找到电子邮件,但据我所知
  • 这听起来可能有点居高临下,但是,也许是一个 if 语句?我在想 contains() 方法或类似方法。我假设您提供的示例文本中的两个示例都进入了大学电子邮件的数组。子域是你的哨兵值,所以条件是, if (theEmail.contains(subdomain)) storeWithUniversityEmails else storeElsewhere

标签: java arrays class


【解决方案1】:

只要您只需要电子邮件地址而不需要任何其他数据,您就可以将它们存储为字符串。

使用列表比使用数组灵活得多。我特别喜欢,列表可以随心所欲……

我写了一个例子,使用Map 作为存储。 Map 通常是一种组织东西的好方法:您可以通过定义“键/值”对在其中存储数据。

在我的示例中,地图使用子域(作为字符串)作为键,使用电子邮件地址列表(再次作为字符串)作为值。

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Try {

    public static void main(String[] args) throws IOException {

        Map<String, List<String>> storage = new HashMap<>();

        // !important! initialize scanner with a file, not with a filename
        Scanner input = new Scanner(new File("inputemails.txt"));

        // a new pattern to get the subdomain out of an eMail-address
        Pattern subdomainPattern = Pattern
                .compile(".*\\@([^\\.]+(?:\\.[^\\.]+)*)(?:\\.[^\\.]+){2,}");

        // I used your pattern to find any eMail-address
        Pattern eMailPattern = Pattern
                .compile("([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4})");

        // Use Scanner to find each match in your input file
        String eMailAddress;
        while ((eMailAddress = input.findWithinHorizon(eMailPattern, 0)) != null) {

            // determine the subdomain of this eMail-address
            String subdomain;
            Matcher matcher = subdomainPattern.matcher(eMailAddress);
            if (matcher.matches()) {
                subdomain = matcher.group(1);
            } else {
                subdomain = "no subdomain";
            }

            // get/create list of all eMail-addresses for this subdomain from
            // map
            List<String> listOfEMailAddresesToThisSubdomain = storage
                    .get(subdomain);
            if (listOfEMailAddresesToThisSubdomain == null) {
                listOfEMailAddresesToThisSubdomain = new ArrayList<>();
                storage.put(subdomain, listOfEMailAddresesToThisSubdomain);
            }

            // add this eMail-address to list
            listOfEMailAddresesToThisSubdomain.add(eMailAddress);
        }

        input.close();

        System.out.println("Choose subdomain");
        System.out
                .println("0.none 1.art 2. chee 3. chem 4. coe 5. cs 6. egr 7. polsci");

        // read, what the user will choose
        char userInput = (char) System.in.read();

        // map the number to a subdomain name
        String chosenDomain;
        switch (userInput) {
        case '0':
            chosenDomain = null;
            break;
        case '1':
            chosenDomain = "art";
            break;
        case '2':
            chosenDomain = "chee";
            break;
        case '3':
            chosenDomain = "chem";
            break;
        case '4':
            chosenDomain = "coe";
            break;
        case '5':
            chosenDomain = "cs";
            break;
        case '6':
            chosenDomain = "egr";
            break;
        case '7':
            chosenDomain = "polsci";
            break;
        default:
            System.out.println("invalid choice");
            return;
        }

        // open file for writing
        PrintWriter output = new PrintWriter("outputemails.txt");

        // use the chosen subdomain name as a key to our storage map. The result
        // will be the list of all eMail-Addresses of that subdomain (or null).
        List<String> eMailAddresses = storage.get(chosenDomain);

        // make output
        if (eMailAddresses == null) {
            System.out.println("no addresses");
        } else {
            for (String eMail : eMailAddresses) {
                System.out.println(eMail);
            }
        }
        output.close();
    }
}

像你这样的输入会导致这样的输出:

cs
  hakan@cs.uh.edu
  tarikul2000@cs.uh.edu

【讨论】:

  • 这很好,但我怎样才能让它打印出不同的子域集?例如,用户输入 1,我只想打印具有艺术域的那些,或者如果输入为 0,那么只有那些没有子域的?
  • 我对地图也不是很熟悉,所以我只了解部分代码。
  • 您的代码运行良好,唯一的事情是我应该为每个电子邮件或 UniversityEmail 对象(如果它有一个子域)创建一个电子邮件对象(一个创建的类)并将它们存储到数组或列表中.是否可以在使用 Map 时执行此操作,或者我是否必须覆盖其他内容?我还将添加我在 OP 中收到的说明以希望澄清
  • 我不知道,我正在做你的作业 :) 说明说,你必须使用ArrayList,所以我认为你必须以这种方式重构我的代码(可能您将不得不删除我的地图)。如果我正确理解了这些要求,那么您可能最终会使用new ArrayList&lt;Email&gt;() 或类似的。
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 2017-09-23
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多