J
J
Justperfect2021-08-17 22:10:30
Java
Justperfect, 2021-08-17 22:10:30

How to correctly set the condition?

Greetings, Khabrovites!
The other day I started learning Java, at the moment I am writing a program that executes commands for adding, editing and deleting a to-do list.

After executing each command, an up-to-date to-do list is displayed at the end. There are two key ones among them:
ADD - Adds the case last on the list
ADD X - Adds the case and assigns it the corresponding serial number.

My problem is that the ADD command (unlike all the others) causes the general to-do list to appear only after re-typing it into the console. Example:
ADD Case 1
You have added: Case 1
ADD Case 2 (or any other crap) To-do
list :

Case 1 Case 2
Case 3
Case
1
You added: Case 2.

I'm guessing the problem is the "boolean input3" variable that I'm using to determine if there is a number in the user's input string or not (to separate the ADD or ADD X methods). I would be grateful for constructive advice. I attach the code below and ask him not to be afraid.

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

public class Loader {
    public static void main(String[] args) {


        ArrayList<String> list = new ArrayList<>(){{
            add("Первое дело");
        }};

        list.add("Второе дело");
        list.add("Третье дело");

        System.out.println("Введите команду " +
                "\n ADD - для добавления дела; " +
                "\n LIST - для вывода списка дела," +
                "\n ADD X - для добавления дела в строку X" +
                "\n DELETE X - для удаления строки" +
                "\n EDIT X - для редактирования строки");

        Scanner scanner = new Scanner(System.in);
        String add = "ADD";
        String printList = "LIST";
        String delete = "DELETE";
        String edit = "EDIT";
        Boolean finish = false;


        while (!finish) {

            String input = scanner.next();

            // ADD
            if (input.equals(add)) {
                boolean input3 = scanner.hasNextInt();
                if (!input3){
                String input2 = scanner.nextLine();
                input2 = input2.replaceAll("ADD", "");
                input2 = input2.trim();
                list.add(input2);
                System.out.println("Вы добавили: " + input2);
            }
        }

            // LIST
            if (input.equals(printList)) {}

            // ADD X
            if (input.equals(add)) {
                boolean input3 = scanner.hasNextInt();
                if (input3) {
                    String input2 = scanner.nextLine();
                    int firstIndex = input2.indexOf(' ');
                    int secondIndex = input2.indexOf(' ', +1);
                    String value = input2.substring(firstIndex, secondIndex).trim();
                    int i = Integer.parseInt(value);
                    String text = input2.substring(secondIndex);
                    boolean comp = i > list.size() + 1;
                    if (comp) {
                        i = list.size();
                        list.add(i, text.trim());
                    }
                    if (!comp) {
                        list.add(i - 1, text.trim());
                    }
                }
            }

            // DELETE X
            if (input.equals(delete)) {
                boolean input3 = scanner.hasNextInt();
                if (input3) {
                    String input2 = scanner.nextLine();
                    int firstIndex = input2.indexOf(' ');
                    String value = input2.substring(firstIndex).trim();
                    int i = Integer.parseInt(value);

                    boolean comp = i > list.size();
                    if (comp) {
                        System.out.println("Неправильное значение! У вас всего " + list.size() + " дел(а)!");
                    }
                    if (!comp) {
                        list.remove(i - 1);
                    }
                }
            }

            // EDIT X
            if (input.equals(edit)) {
                boolean input3 = scanner.hasNextInt();
                if (input3) {
                    String input2 = scanner.nextLine();
                    int firstIndex = input2.indexOf(' ');
                    int secondIndex = input2.indexOf(' ', +1);
                    String value = input2.substring(firstIndex, secondIndex).trim();
                    int i = Integer.parseInt(value);
                    String text = input2.substring(secondIndex);

                    boolean comp = i > list.size();
                    if (comp) {
                        System.out.println("Неправильное значение! У вас всего " + list.size() + " дел(а)!");
                    }
                    if (!comp) {
                        list.remove(i - 1);
                        list.add(i - 1, text.trim());
                    }
                }
            }

            if (input.equals("STOP")) {
                finish=true;
            }

            System.out.println("Список дел: \n");
            for (String print : list) {
                System.out.println(print);
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Cheremisin, 2021-08-17
@leahch

If you started to study, then start with enumerations - enums and the switch / case statement

N
Nazriaz, 2021-08-18
@Nazriaz

Inside 2 if loop with same condition input.equals(add)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question