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.
input = s.nextInt(); s.nextLine();