vendredi 31 juillet 2015

Reading A File and Storing It In An Object

I am trying to read from a file and store the contents into an object called ToDoList(from what I assume is under the GetItem method). Then I am supposed to allow the user to add on to the list. But I am lost on how to create the object and print it.

public class ToDoList {

private ToDoItem[] items;

ToDoItem td = new ToDoItem();
String inputline;
Scanner keyboard = new Scanner(System.in);

int i = 0;

String[] stringArray = new String[100];



private void setItems(ToDoItem[] items) throws FileNotFoundException {
    File file = new File("ToDoItems.txt");
    Scanner ReadFile = new Scanner(file);

    while (ReadFile.hasNext()) {
        String ListString = ReadFile.nextLine();
        stringArray[100] = (ListString);
    }
}

private ToDoItem[] getItems() {

    return items;
}

public void addItem(int id, String description) {
    stringArray[100] = (td.getId() + td.getDescription());

}

public String[] getAddItem() throws FileNotFoundException {

    try (PrintWriter fout = new PrintWriter(new File("ToDoItems.txt"))) {
        do {
            System.out.println("add to the list? [y/n]");
            inputline = keyboard.nextLine();

            if ("y".equals(inputline)) {
                i++;
                stringArray[i] = (td.getId() + ". " + td.getDescription() + "\n");
                fout.print(stringArray[i]);
            } else {

                System.out.println("Here is the list so far:");

            }
        } while ("y".equals(inputline));
        return stringArray;
    }
}

@Override
public String toString() {
    return "ToDoList{" + "items=" + getItems()
            + '}';
}

I am supposed to use the "getAddItem" method to allow the user to add to the list. But I can't figure out how to add an array to an object. let alone make the object.

Aucun commentaire:

Enregistrer un commentaire