0

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.

3
  • I'm not an expert, but why dont you use switch case instead of if else if.. Commented Oct 11, 2014 at 5:18
  • I've heard about a switch case but I have not learned it yet though its a priority of mine so I will learn it very soon. Thanks for the suggestion. Commented Oct 11, 2014 at 5:20
  • Consume nextLine() after calling nextInt(). Then you won't need two Scanner instances. e.g. input = s.nextInt(); s.nextLine(); Commented Oct 11, 2014 at 5:42

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome dude, fixed the issue, I am new to constructors but I kind of get what you mean, thanks for the help!
@Tacent , if you got the solution , you should accept the answer. Just click on the correct sign.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.