Please help, this is due Monday! I will provide the ArrayListDemo.java at the bo
ID: 3547807 • Letter: P
Question
Please help, this is due Monday! I will provide the ArrayListDemo.java at the bottom. Thank you! Exercise 1 1. Create a New Java Application named ArrayListDemo and copy the file ArrayListDemo.java into the project. 2. Compile and run the program. 3. Modify the program so that it asks the user to input a String after the list has been input and tells the user if that String exists in the list. 4. Modify the program so that if the String in 3 has been found, it asks the user to input another String and it replaces the original String with the new String. Print out the list after the String has been replaced. Sample output: Enter items for the list, when prompted. Type an entry: Alice More items for the list? yes Type an entry: Bob More items for the list? yes Type an entry: Carol More items for the list? no The list contains: Alice Bob Carol Enter a String to search for: Bob Enter a String to replace with: Bill Bob found! The list contains: Alice Bill Carol Sample output: Enter items for the list, when prompted. Type an entry: Alice More items for the list? yes Type an entry: Bob More items for the list? yes Type an entry: Carol More items for the list? no The list contains: Alice Bob Carol Enter a String to search for: Bill Bill not found! The list contains: Alice Bob Carol ArrayListDemo.java: import java.util.ArrayList; import java.util.Scanner; public class ArrayListDemo { public static void main(String[] args) { ArrayList toDoList = new ArrayList(); System.out.println("Enter items for the list, when prompted."); boolean done = false; Scanner keyboard = new Scanner(System.in); while (!done) { System.out.println("Type an entry:"); String entry = keyboard.nextLine( ); toDoList.add(entry); System.out.print("More items for the list? "); String ans = keyboard.nextLine( ); if (!ans.equalsIgnoreCase("yes")) done = true; } System.out.println("The list contains:"); int listSize = toDoList.size( ); for (int position = 0; position < listSize; position++) System.out.println(toDoList.get(position)); /* Alternate code for displaying the list System.out.println("The list contains:"); for (String element : toDoList) System.out.println(element); */ } }Explanation / Answer
I have tried answering it to the point.....including the SOP's also..
The output which I got after running the application :
Enter items for the list, when prompted.
Type an entry:
alice
More items for the list? yes
Type an entry:
bob
More items for the list? yes
Type an entry:
carol
More items for the list? no
The list contains:
alice
bob
carol
String to search for:
alice
Enter a String to replace with:
shara
alice found !
More items for the list? no
Now the list contains:
bob
carol
shara
Code :
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList toDoList = new ArrayList();
System.out.println("Enter items for the list, when prompted.");
boolean done = false;
boolean searchAndReplace = false;
Scanner keyboard = new Scanner(System.in);
while (!done) {
System.out.println("Type an entry:");
String entry = keyboard.nextLine( );
// code for point 3 of question
if(toDoList.contains(entry)){
System.out.println("Entry already exist in the list, please enter another value");
continue;
}
toDoList.add(entry);
System.out.print("More items for the list? ");
String ans = keyboard.nextLine( );
if (!ans.equalsIgnoreCase("yes"))
done = true;
}
System.out.println("The list contains:");
int listSize = toDoList.size( );
for (int position = 0; position < listSize; position++){
System.out.println(toDoList.get(position));
}
while (!searchAndReplace) {
System.out.println("String to search for:");
String search = keyboard.nextLine( );
System.out.println("Enter a String to replace with:");
String replace = keyboard.nextLine( );
// code for point 3 of question
if(toDoList.contains(search)){
System.out.println(search + " found !");
toDoList.remove(search);
toDoList.add(replace);
}else {
System.out.println(search + " not found !");
}
System.out.print("More items for the list? ");
String ans = keyboard.nextLine( );
if (!ans.equalsIgnoreCase("yes"))
searchAndReplace = true;
}
System.out.println("Now the list contains:");
int listSizeNew = toDoList.size( );
for (int position = 0; position < listSizeNew; position++){
System.out.println(toDoList.get(position));
}
}
}