Java 将数据存储在数组中并输出该数据

Java 将数据存储在数组中并输出该数据,java,arrays,Java,Arrays,我正在开发一个程序,该程序将来自用户的数据存储在一个数组中并输出该数据 例如: 输入: Happy HAPPY@foo.com 输出: NAME: Happy EMAIL: HAPPY@foo.com 我希望有人能看看我到目前为止所得到的,并给我一个如何继续下去的指针。我知道我必须使用scanner类和scan.nextLine,我不确定接下来会发生什么。我知道我没有太多,我不想找人来完成这件事,但也许有人可以给我一些建议,或者给我指出正确的方向。我相信我的计划有正确

我正在开发一个程序,该程序将来自用户的数据存储在一个数组中并输出该数据

例如:

输入:

Happy HAPPY@foo.com
输出:

 NAME:  Happy        
 EMAIL: HAPPY@foo.com 
我希望有人能看看我到目前为止所得到的,并给我一个如何继续下去的指针。我知道我必须使用scanner类和scan.nextLine,我不确定接下来会发生什么。我知道我没有太多,我不想找人来完成这件事,但也许有人可以给我一些建议,或者给我指出正确的方向。我相信我的计划有正确的基础

到目前为止,我的代码是:

    import java.util.ArrayList;
    import java.util.Scanner;

    public class Program5 {

      void loadContacts()
      {
        Scanner scan = new Scanner(System.in);
        System.out.println(scan.nextLine());

        scan.close();
      }

      void printContacts()
      {

      }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Program5 program5 = new Program5();
        program5.loadContacts();
        program5.printContacts();

    }

}

您应该有一个全局变量来存储名称和电子邮件。尝试在代码顶部添加这些行。在
公共类程序5{
之后

private String Name, Email;
必须将此值分配到
void loadContacts()
。拆分读取的字符串

void loadContacts()
{
    Scanner scan = new Scanner(System.in);
    String input = scan.nextLine();
    String arr[] = input.split("\\s+");
    Name = arr[0];
    Email = arr[1];
    scan.close();
}
最后是关于
void printContacts()

下面是运行的代码:

最好将类命名为“person”或类似的名称,但不必解释:

public class Program5 {
    private String name;
    private String mail;

    public Program5(){}

    void loadContacts(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a name and a mail like this : name email@email.com (separate with ' ')");
        String[] line = scan.nextLine().split(" ");
        while(line.length!=2){
            System.out.println("Again, enter a name and a mail like this : name email@email.com (separate with ' ')");
            line = scan.nextLine().split("/");
        }
        this.setName(line[0]);
        this.setMail(line[1]);
        scan.close();
    }

    void printContacts() {
        System.out.println("NAME : "+this.name+"\nEMAIL : "+this.mail);
    }

    public static void main(String[] args) {
        Program5 program5 = new Program5();
        program5.loadContacts();
        program5.printContacts();
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }
}

在加载中,您要求用户选中它,他输入2个元素,以“/”分隔,如果是,则将它们存储为属性,以便能够用另一种方法获取它们;)

您可以执行多种操作

  • 您可以创建
    loadContacts()
    printContacts()
    静态方法,还可以更改
    loadContacts()
    以使其返回数组或二维数组,或者以您选择的方式表示名称电子邮件对。然后更改
    print
    Contacts()`接受该类型并遍历该数组以打印出每个名称/电子邮件对。此解决方案需要更多的工作,但不必在该类的主方法中创建同一类的对象
  • 您可以保持方法不变,而是为程序类创建一个名为
    contacts
    的新字段,它将是您选择用于表示姓名/电子邮件对的类型。您可以在
    loadContacts()
    中向
    contacts
    添加项目,并在
    printContacts()中对其进行迭代
    。这样您就不必更改主方法中的任何内容

  • “我现在迷路了。”-确切的位置和方式。请提供有关您到底在寻找什么的更多详细信息。将您扫描的内容存储在类级数组中。然后在数组中的项目上迭代,并附加“名称”或“电子邮件”根据您的要求。您可能希望向字符串添加验证,但这取决于您。开始尝试将从nextLine读取的字符串存储到变量中,并将文本分为两部分(名称和电子邮件)。用“”分隔是什么意思?例如“doc”doc@foo.com'这是一个空格,就像你的输入示例一样
    public class Program5 {
        private String name;
        private String mail;
    
        public Program5(){}
    
        void loadContacts(){
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter a name and a mail like this : name email@email.com (separate with ' ')");
            String[] line = scan.nextLine().split(" ");
            while(line.length!=2){
                System.out.println("Again, enter a name and a mail like this : name email@email.com (separate with ' ')");
                line = scan.nextLine().split("/");
            }
            this.setName(line[0]);
            this.setMail(line[1]);
            scan.close();
        }
    
        void printContacts() {
            System.out.println("NAME : "+this.name+"\nEMAIL : "+this.mail);
        }
    
        public static void main(String[] args) {
            Program5 program5 = new Program5();
            program5.loadContacts();
            program5.printContacts();
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setMail(String mail) {
            this.mail = mail;
        }
    }