简体   繁体   English

从阵列列表中打印特定部分

[英]Printing a specific part from an Array List

So i have an Arraylist which is holding a class as shown from the code below. 所以我有一个Arraylist,它持有一个类,如下面的代码所示。

NewStorage.java is the name of the file which contains the code shown below NewStorage.java是包含以下代码的文件的名称

package postit;

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

public class NewStorage extends NoteInfomation {

    Scanner inputID = new Scanner(System.in);
    Scanner NoteID = new Scanner(System.in);

    int count = 0;

    ArrayList<NoteInfomation> Note = new ArrayList<NoteInfomation>(20);

    public void printinfo() {
        System.out.println("--- Fill note here  ---");
    }

    public void Notestore() {

        System.out.println("Enter the note ID you wish to attach the note with\n\n");
        inputIDnote = inputID.nextLine();
        System.out.println("Enter your note\n\n");
        noteDescription = NoteID.nextLine();
        System.out.println("" + inputIDnote);
        System.out.println("" + noteDescription);
    }

    public void PrintNotes() {

        for (int i = 0; i < Note.size(); i++) {
            System.out.println(" " + Note.get(i));

        }
    }
}

This is the main file named Postit.java shown below 这是名为Postit.java的主文件,如下所示

package postit;

import java.util.Scanner;

public class Postit {

    public static void main(String[] args) {
        int MenuOption = 0;

        NewStorage G = new NewStorage();    // Case 1 Object

        while (MenuOption != 3) {

            System.out.println(
                    "\n--------Note System-------\n" +
                            "----------------------------\n" +
                            "1.   Create a Note \n" +
                            "2.   View Notes \n" +
                            "3.   Close Program\n" +
                            "----------------------------\n"
            );

            Scanner menu = new Scanner(System.in);
            MenuOption = menu.nextInt();

            switch (MenuOption) {
                case 1:
                    G.printinfo();
                    G.Notestore();
                    break;
                case 2:
                    G.PrintNotes();
                    break;
                case 3:
                    System.out.println("Program is closing");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice.");
                    break;
            }
        }
    }
}

Finally this is the note.java file which contains the NoteInfomation class which is used for the array. 最后,这是note.java文件,其中包含用于数组的NoteInfomation类。

package postit;

class NoteInfomation {
    String inputIDnote;
    String noteDescription;
}

So, my question is, as shown in the code the program is supposed to ask the user what note ID they would like to assign their note too. 因此,我的问题是,如代码中所示,程序应该询问用户他们也想为其分配便笺的便笺ID。 Then the user can view the note they created by entering the corresponding note ID. 然后,用户可以通过输入相应的注释ID来查看他们创建的注释。 Ideally, as I build upon the program I would also like the program to let the user choose from a list of ID's which have previously been created to remember the ID for created notes wouldnt be an issue. 理想情况下,当我以该程序为基础时,我还希望该程序让用户从以前创建的ID列表中进行选择,以记住创建的便笺的ID不会成为问题。 Thanks in advance. 提前致谢。

There are lots of bugs in your code. 您的代码中有很多错误。 You are not storing any note in your Notestore() method. 您没有在Notestore()方法中存储任何笔记。 PrintNode() will not work either. PrintNode()也不起作用。

Change your code to something like this.. 将您的代码更改为这样。

NoteInfomation.java 注意Infomation.java

class NoteInfomation {
    String inputIDnote;
    String noteDescription;
    public NoteInfomation(String inputIDnote, String noteDescription) {
        this.inputIDnote = inputIDnote;
        this.noteDescription = noteDescription;
    }
    @Override
    public String toString() {
        return "Id:" + inputIDnote + ", Description: " + noteDescription;
    }
}

Nodestore() method... Nodestore()方法...

public void Notestore() {

    System.out.println("Enter the note ID you wish to attach the note with\n\n");
    String inputIDnote = inputID.nextLine();
    System.out.println("Enter your note\n\n");
    String noteDescription = inputID.nextLine();  //No need for two scanner
    System.out.println("" + inputIDnote + "\n" + noteDescription);
    Note.add(new NoteInfomation(inputIDnote, noteDescription));
}

PrintNotes() method PrintNotes()方法

public void PrintNotes() {

    for (int i = 0; i < Note.size(); i++) {
        System.out.println(" " + Note.get(i).toString());

        //**or you can also use this
        //  System.out.println("Id:" + Note.get(i).inputIDnote + ", Description: " + Note.get(i).noteDescription);

    }
}

I encourage you study how ArrayList works. 我鼓励您学习ArrayList的工作方式。

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

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