简体   繁体   中英

Printing a specific part from an Array List

So i have an Arraylist which is holding a class as shown from the code below.

NewStorage.java is the name of the file which contains the code shown below

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

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.

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. Then the user can view the note they created by entering the corresponding note 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. Thanks in advance.

There are lots of bugs in your code. You are not storing any note in your Notestore() method. PrintNode() will not work either.

Change your code to something like this..

NoteInfomation.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...

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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