has a problem when performing a task: I can not implement the methods of the LIST collection in accordance with the terms of the problem. Please help figure out mistakes and suggest decisions, if possible!
Tasks:
Using Scanner Scanner = New Scanner (System.in) and Scanner.NextLine (), in an infinite loop, you need to sequentially get the names of the affairs or word END. If the word end is not entered, then save the entered value to the Task variable String String Task = Scanner.NextLine () and add it to the list list. If the word end is entered, then using the for (String Task: List) design, take the task into the console. You can also withdraw the index, and using the local counter.
After displaying a list of cases into the console, the program gives the user the ability to delete a certain case from the list on the index. This happens in an infinite loop until the user enters the keyword “finish”.
Add the check if the value entered the condition to exit the FINISH row.
Using the ArrayList Remove (int index) method, delete the case from the list. Please note that it will be necessary to translate String in Int.
If the FINISH word is entered, then using the for design (String Task: List), output tasks into the console along with the index. Make sure that the case was correctly removed from the list and indexes were shifted to the left.
Here is my code:
Public Static Void Main (String [] Args) {
Scanner Scanner = New Scanner (System.in);
ArrayList & LT; String & GT; List = New ArrayList & LT; & GT; ();
While (True) {
System.out.printLN ("Enter the name of the task (to complete, enter 'END'):");
System.Out.printLN ("1. Add task \ N2. Display a list of tasks \ N3. Remove the task \ N0. Exit");
String Input = scanner.nextline ();
if (input.equals ("1")) {
System.out.PrintLN ("Enter the task for planning:");
String Task = Scanner.NextLine ();
List.add (Task);
} ELSE if (input.equals ("2")) {
System.Out.printLN ("List of tasks: \ n" + list);
Scanner.NextLine ();
} ELSE if (input.equals ("finish")) {
int task = integer.paraseint (task);
} ELSE If (input.equals ("END")) {
For (String Task: List) {
}
}
}
Answer 1
Try this:
import java.util.arraylist;
Import java.util.list;
Import java.util.scanner;
Public Class Main {
Private Final Static List & Lt; String & GT; Cases = New ArrayList & lt; & gt; ();
Public Static Void Main (String [] Args) {
Scanner Scanner = New Scanner (System.in);
While (True) {
System.out.printLN ("Enter the name of the task (to complete, enter 'END'):");
String Input = scanner.nextline ();
if ("END" .equals (Input)) {
For (int i = 0; i & lt; cases.size (); i ++) System.out.printLN (I + "- & gt;" + Cases.get (I));
System.out.PrintLN ("Enter the index to delete the task or press any button to continue:");
RemoveFexist (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 (NumberFormateException | IndexOUTOFBOUNDSEXCEPTION E) {
RETURN FALSE;
}
}
}