Running ArrayList and BufferedReader Program

I created a simple program that accepts a title and a note that you enter, then you have the choice to export the notes to a txt file using BufferedWriter

, however, since each note is an object that is stored in ArrayList

while storing them, I iterate through a forced loop, it duplicates every note as I iterate over the whole object.

Annotation class

import java.util.*;
public class Notes
{ 
    private String notes;
    private String titleOfNotes;
    Scanner input = new Scanner(System.in);

    public Notes()
    {
        titleOfNote(input);
        takeNotes(input);
    }

    public void takeNotes(Scanner x)
    {
        System.out.println("Please Enter Your Note");
        notes = x.nextLine();
    }

    public void titleOfNote(Scanner y)
    {   
        System.out.println("Please Enter Title");
        titleOfNotes = y.nextLine();
    }
    public String toString()
    {
        return "Title: " + titleOfNotes + "\t" + notes; 
    }

}

      

Application class // Does most of the work

import java.util.*;
import java.io.*;
public class App
{
    private int exit = 0; 
    private int createANote;
    private int displayTheNotes; 
    private int inputFromUser;
    public boolean haveFileBeenWritten = true;

    File file = new File("Notes.txt");

    Scanner input = new Scanner(System.in);

    ArrayList<Notes> arrayOfNotes = new ArrayList<Notes>();

    public void makeNoteObject()
    {
        arrayOfNotes.add(new Notes());  
    }

    public void displayAllTheNote(ArrayList<Notes> n)
    {
            for(Notes singleObjectOfNote : n)
            {
                System.out.println(singleObjectOfNote);
            }
    }

    public void programUI(){


        while(exit != 1)
        {   
            System.out.println("1. Create A Note");
            System.out.println("2. Display The Notes");
            System.out.println("3. Exit");
            System.out.println("4. Export to text file");
            System.out.println("Enter Your Operation");
            inputFromUser = input.nextInt();

            if(inputFromUser == 1)
            {
                makeNoteObject();
            }
            else if(inputFromUser == 2)
            {
                displayAllTheNote(arrayOfNotes);
            }
            else if(inputFromUser == 3)
            {   
                System.out.println("Exited");
                exit = 1;
            }
            else if(inputFromUser == 4)
            {
                makeATxtFileFromNotes(arrayOfNotes); 
                System.out.println("Textfile created filename: " + file.toString()); 
            }
            else
            {
                System.out.println("You Select A Invalid Command");
            }
        }
    }

    public void makeATxtFileFromNotes(ArrayList<Notes> x)
    {

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file,haveFileBeenWritten)))
        {
//Problem here!
            for(Notes singleObjectOfNotes : x)
            {
                bw.write(singleObjectOfNotes.toString());
                bw.newLine(); 
            }


        }catch(IOException e)
        {
            System.out.println("Cant Write File: " + file.toString());
            haveFileBeenWritten = false;
        }
    }

    public App()
    {
        programUI();
    }
    public static void main(String[]args)
    {
        App objectOfApp = new App();

    }
}

      

I'm new to Java so my code won't be the best!

+3


source to share


1 answer


If your problem is that you only need to see the current list Notes

excluding the previous', it is because of this line:

try (BufferedWriter bw = new BufferedWriter(new FileWriter(file,haveFileBeenWritten)))

      

By default it haveFileBeenWritten

is true

, so based on the FileWriter API, it will ATTACH to an existing file Notes.txt

, so if you don't want that, change it to false

.

Parameters:

file - the File object to write to

add - if true, then bytes will be written to the end of the file, not the beginning



EDIT . To access items List<>

use get()

.

Example:

int size = myList.size();
for (int i = 0 ; i < size ; i++) {
    //...
    Notes note = myList.get(i);
    //...
}

      

+1


source







All Articles