I can't implement List collection methods in JAVA

Encountered a problem when executing the task: I can't implement the methods of the List collection in accordance with the task conditions. Please help me understand the errors and suggest solutions, if possible!

Task conditions:

Using Scanner scanner = new Scanner(System.in) and scanner. nextLine(), in an infinite loop, you must sequentially get the names of the cases or the word end.If the word end is not entered, save the entered value to the task variable type String String task = scanner.nextLine () and add it to the list. If the word end is entered, then using the for (String task : list) construction, we output the tasks to the console. You can also output the index using a local counter. After displaying the to-do list in the console, the program gives the user the opportunity to delete a specific task from the list by index. This happens in an infinite loop until the user enters the keyword "Finish". Add a check to see if the entered value is equal to the condition for exiting the loop is the Finish line. Using the ArrayList remove(int index) method, remove the case from the list. Note that you will need to translate String to int. If the word Finish is entered, then, using the for (String task : list) construction, output the tasks to the console along with the index. Make sure that the case was correctly removed from the list and the indexes were shifted to the left.

Here is my code:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    ArrayList<String> list = new ArrayList<>();
    
    while(true) {
      System.out.println("Введите название задачи (для завершения введите 'end'):");
      System.out.println("1. Добавить задачу\n2. Вывести список задач\n3. Удалить задачу\n0. Выход");
      String input = scanner.nextLine();
      if (input.equals("1")) {
        System.out.println("Введите задачу для планирования:");
        String task = scanner.nextLine();
        list.add(task);
      } else if (input.equals("2")) {
        System.out.println("Список задач:\n" + list);
        scanner.nextLine();
      } else if (input.equals("Finish")) {
        int task = Integer.parseInt(task);
      

      } else if (input.equals("end")) {
        for (String task : list) {

    }
  }
}
Author: Roman Konoval, 2020-07-21

1 answers

Try this way:

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

public class Main {
    
    private final static List<String> CASES = new ArrayList<>();

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("Введите название задачи (для завершения введите 'end'):");            
            String input = scanner.nextLine();
            if ("end".equals(input)) {
                for (int i = 0; i < CASES.size(); i++) System.out.println(i + " -> " + CASES.get(i));
                System.out.println("Введите индекс для удаления задачи либо нажмите любую кнопку для продолжения : ");
                removeIfExist(scanner.nextLine());
                continue;
            }
            if ("Finish".equals(input)) break;
            CASES.add(input);
        }
        
    }
    
    private static boolean removeIfExist(String index) {
        try {
            int i = Integer.valueOf(index);
            CASES.remove(i);
            return true;
        } catch (NumberFormatException | IndexOutOfBoundsException e) {
            return false;
        }
    }
    
}
 0
Author: Дмитрий, 2020-07-21 10:52:04