java - Create an ArrayList then Search the List and allow the user to change part of the list or not -
our assignment create to-do list using arraylist coding.
then change code ask user input string after list has been inputted, tell user if string exists in list.
finally, if string has been found allow user input string , replaces original string. print out list.
here have: unsure how continue.
import java.util.arraylist; import java.util.scanner; public class arraylistdemo { public static void main(string[] args) { arraylist<string> todolist = new arraylist<string>(); system.out.println("enter items list, when prompted."); boolean done = false; scanner keyboard = new scanner(system.in); while (!done) { system.out.println("type entry:"); string entry = keyboard.nextline( ); todolist.add(entry); system.out.print("more items list? "); string ans = keyboard.nextline( ); if (!ans.equalsignorecase("yes")) done = true; } system.out.println("the list contains:"); int listsize = todolist.size( ); (int position = 0; position < listsize; position++) system.out.println(todolist.get(position)); ) )
i notice incorporate:
arraylist<string> searchlist = new arraylist<string>(); string search = "a"; int searchlistlength = searchlist.size(); (int = 0; < searchlistlength; i++) { if (searchlist.get(i).contains(search)) { //where put after list printed or before? appricated } }
here sample output of trying do:
enter items list, when prompted. type entry: alice more items list? yes type entry: bob more items list? yes type entry: carol more items list? no list contains: alice bob carol enter string search for: bob enter string replace with: bill bob found! list contains: alice bill carol
if user searches item not found tells them "item" not found!
i'll use indexof()
find index, , if item exists(index != -1
) you'll index can use replace item using set(int index, e item)
.
as example, can like..
boolean search=true; while(search) { system.out.print("search.."); string searchfor = keyboard.nextline(); int ind = searchlist.indexof(searchfor); if (ind == -1) { system.out.println("not found"); } else { system.out.print("replace with.. "); string replacewith = keyboard.nextline(); searchlist.set(ind, replacewith); } system.out.print("continue searching.. "); string ans = keyboard.nextline( ); if (!ans.equalsignorecase("yes")) search = false; }
Comments
Post a Comment