Answer the question
In order to leave comments, you need to log in
How to properly organize the call of arguments in the console?
Dear users of the site Habr Q&A.
I am writing to you because I am in despair.
I started learning Java
, everything was fine until I ran into a task. which I can not do for almost a month!
fucked up the whole internet. studied textbooks. but I did not find a clear solution to the problem.
the teacher scored a bolt and cannot (or does not want to) explain anything (
I understand that for someone (most likely for the majority) this will seem elementary.
but at the moment, for me, this task is very difficult!
I wrote the code, but completely I don't understand how to fix it
.
Develop a to-do list that can be controlled by commands in the console. LIST, ADD, EDIT, DELETE commands. LIST should output the cases with their serial numbers. ADD - add a case to the end of the list or a case to a certain place, moving the rest of the cases forward if you specify a number. EDIT - replace the case with the specified number. DELETE - delete. Command examples:
LIST
ADD Some case
ADD 4 Some case in fourth place
EDIT 3 New case name
DELETE 7
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.InputMismatchException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws IOException {
System.out.println("Список команд: LIST открывает список, ADD добавляет что-то в список, DELETE удаляет что-то из списка");
System.out.println("Вызовите команду : ");
ArrayList<String> todoList = new ArrayList<>() {{
add(0, "Почистить зубы");
add(1, "Сделать зарядку");
add(2, "Выпить стакан воды");
add(3, "Приготовить завтрак");
add(4, "Отправиться на работу");
}};
while (true) {
Scanner sc = new Scanner(System.in);
switch (sc.nextLine()) {
case "LIST":
System.out.println("Ваш список дел: " + todoList);
continue;
case "ADD":
System.out.println("Добавить дело в список: ");
todoList.add(sc.nextLine());
System.out.println("Дело добавленно!" + " номер в списке: " + todoList.size());
continue;
case "DELETE":
System.out.println("Удалить дело из списка: ");
todoList.remove(sc.nextInt() - 1);
System.out.println("Дело удалено! " + todoList.size());
continue;
default:
System.out.println("Давайте выберем действие!");
break;
}
sc.close();
}
}
}
Answer the question
In order to leave comments, you need to log in
It would be great if you wrote down what specifically doesn't work in your code.
Of those errors that I noticed:
sc.nextLine
() - returns the line entered by the user,
therefore
the command: ADD 4 at the very least, after receiving the sc.nextLine() line, you need to save it into a variable, parse it, extract commands and parameters from it, and only then describe your switch
. And now, in theory, only LIST works for you, and for everything else you get " let's choose an action"
The first thing I would recommend: associated arrays.
The second thing I would recommend is to forget the first point and look towards SQL or NoSQL.
Third: the implementation of the arguments in this example should be more adequate.
Greetings)
In fact, this is all quite simply implemented. I agree with Konstantin about data storage. You can use SQLite to store data or write to some txt or any other file. And then, when accessing the loop, get the stored information.
As for the commands, have you tried looking for ready-made libs?
Hint - JCommander
jcommander.org
It remains to come up with your own commands and implement the task.
As an option - a more complicated option is to use Spring and implement a console application using jCommander and use the hibernate + jpa capabilities to implement an ORM for ease of handling the database...
There may be more interesting implementation options, but I would use JCommander
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question