简体   繁体   中英

Java ArrayList printing out empty?

My problem is simple. When sending the value of x when input == 1, the method is supposed to add the value of x into the al ArrayList. It does this, but when I attempt to print the values of al , it prints out empty.

First class:

import java.util.Scanner;

public class ToDo {

    Scanner s = new Scanner(System.in);
    Scanner g = new Scanner(System.in);

    void start() {
        IncompleteTasks incompletetasks = new IncompleteTasks();
        CompleteTasks completetasks = new CompleteTasks();
        AllTasks alltasks = new AllTasks();
        int input = 999;
        String x = "";

        while (input != 0) {

            System.out.println("What would you like to do? Type '0' to cancel");
            System.out.println();
            System.out.println("1. Add a task"); // done
            System.out.println("2. View current tasks");
            System.out.println("3. Delete a task"); // done
            input = s.nextInt();
            if (input == 1) {
                while (!x.equals("quit")) {
                    System.out.print("Enter a task: (Type 'quit' to finish! ");
                    x = g.nextLine();
                    if (x.equals("quit")) {
                        start();
                    }
                    else {
                        incompletetasks.IncompleteTasksAdd(x);
                    }

                }

            }
            else if (input == 2) {
                System.out.println("\t1. All  Tasks");
                System.out.println("\t2. Complete Tasks");
                System.out.println("\t3. Incomplete Tasks");
                input = s.nextInt();
                if (input == 1) {
                    alltasks.AllTasks();
                }
                else if (input == 2) {
                    completetasks.CompleteTasks();
                }
                else if (input == 3) {
                    incompletetasks.IncompleteTasksDisplay();
                }
                else if (input == 0) {
                    System.exit(0);
                }

                else {
                    System.out.println("\t\t\t\tInvalid choice! Try again!");
                    start();
                }
            }

            else if (input == 3) {
                System.out.println("hello");
                x = s.nextLine();
                incompletetasks.IncompleteTasksDelete(x);
            }
            else if (input == 0) {
                System.exit(0);
            }
            else {
                System.out.println("\t\t\t\tInvalid choice! Try again!");
                start();
            }
        }
    }
}

Second Class:

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

public class IncompleteTasks {

    int counter = 0;
    Scanner g = new Scanner(System.in);
    ArrayList<String> al = new ArrayList<String>();

    void IncompleteTasksDisplay() {
        System.out.println("----------------------------------------------------------");
        for (String f : al) {
            counter++;
            System.out.println(f);
            // System.out.println(counter+". " +al.get(f));
        }
        System.out.println("----------------------------------------------------------");

    }

    void IncompleteTasksAdd(String x) {
        al.add(x);
        System.out.println("\tTask Added!");
    }

    void IncompleteTasksDelete(String x) {
        for (int k = 0 ; k < al.size() ; k++) {
            if (al.get(k) == x) {
                al.remove(x);
            }
        }
    }
}

Result:

What would you like to do? Type '0' to cancel

1. Add a task
2. View current tasks
3. Delete a task
1
Enter a task: (Type 'quit' to finish! Test
Task Added!
Enter a task: (Type 'quit' to finish! Test 1
Task Added!
Enter a task: (Type 'quit' to finish! Test 2
Task Added!
Enter a task: (Type 'quit' to finish! quit
What would you like to do? Type '0' to cancel

1. Add a task
2. View current tasks
3. Delete a task
2
1. All  Tasks
2. Complete Tasks
3. Incomplete Tasks
3
----------------------------------------------------------
----------------------------------------------------------
What would you like to do? Type '0' to cancel

1. Add a task
2. View current tasks
3. Delete a task

I am new to java, so don't be surprised if I have bad coding in other sections or if I'm missing something very obvious. I appreciate all the help I can get, as I'm stumped, I don't know what I'm doing wrong.

This line:

IncompleteTasks incompletetasks = new IncompleteTasks();

declares a local variable incompletetasks , specific to the current call to start() , and initializes it to a new empty list of tasks.

This line:

start();

creates a new, separate call to start() , which will therefore have a separate incompletetasks variable, and will not have any information about the incompletetasks variable that you've been adding everything to.

There's no real reason that start() should need to call itself.

The quickest way to fix this is to change this:

while (input != 0){

to this:

mainloop: while (input != 0) {

(where mainloop is a label , giving the while-loop a name so that you can refer to it) and almost every occurrence of this:

start();

to this:

continue mainloop;

(meaning roughly, "go back to the beginning of the loop named mainloop ").

A better fix is to split out your method into smaller parts. That will allow for much clearer code. In general, it's usually best for a method not to have nested loops (though there are plenty of exceptions, so don't take that as a hard rule).

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