Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java - ArrayList Demo

flamenca91
flamenca91 


Exercise 1 1. Create a New Project 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 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); */ } } Attachments
Tags Clarifications

Answers

Java - ArrayList Demo - Solution

dickersonka
dickersonka 



Attached is the solution to the Java - ArrayList Demo assignment including source and comments Attachments
ArrayListDemo.java (1 K) Download Preview
Alternate code for displaying the list System.out.println("The list contains:"); for (String element : toDoList) System.out.println(element); */ //prompt for string to search System.out.println("Enter a string to search for"); String searchString = keyboard.nextLine(); int matchIndex = -1; //loop through list

ArrayListDemo_Screenshot.png (10 K) Download Preview

Screenshots
Screenshot
Purchase Answer

ArrayList - Solution

mariobeka
mariobeka 



Below you will find the ArrayListDemo.java file with the implemented code that was asked.The code is also commented for better understanding. Also screenshot of the program running are provided.
Attachments
Screen Shot 2014-05-05 at 9.49.36 PM.png (51 K) Download Preview

Screen Shot 2014-05-05 at 9.50.08 PM.png (48 K) Download Preview

ArrayListDemo.java (1 K) Download Preview
position < listSize; position++) System.out.println(toDoList.get(position)); // ask the user for a string to search System.out.println("Enter a String to search for:"); String searchString = keyboard.nextLine(); // search for the input in the todolist for (int index = 0; index < listSize; index++) { // check if todolist contains the input

Screenshots
Screenshot
Screenshot
Purchase Answer