java - ArrayList<String> delete query -


here's output:

-----query----- [1]update [2]delete [3]search [4]show choose query:1 enter student id:1 enter first name: respo enter middle name: topher enter last name: raspo want query?(yes/no) yes -----query----- [1]update [2]delete [3]search [4]show choose query: 4 12 christopher reposo porras 1 respo topher raspo 

as can see in picture i'm trying make simple little system without database using arraylist contain data problem in delete query. in delete query tell user type student number 1 delete information of , contain first name, middle name, last name don't have logic in arraylist such thing. way possible use 1 arraylist in case or need make many array list solve problem.

public static void main(string[] args) {     //initialize scanner input process     scanner scan = new scanner(system.in);      //initialize needs variable     list<string> list = new arraylist<string>();     int choose,choosequery;     string chooseyesorno = " ";     string chooseyesorno2 = " ";          {         //startup program         system.out.println("=====-----librarysystem-----=====");         system.out.println("[1]student information");         system.out.println("[2]book information");         system.out.print("choose table:");         choose = scan.nextint();          {                         if(choose == 1) {                 system.out.println("-----query-----");                 system.out.println("[1]update");                 system.out.println("[2]delete");                 system.out.println("[3]search");                 system.out.println("[4]show");                 //reserved                 //reserved                 system.out.print("choose query:");                 choosequery = scan.nextint();                  if(choosequery == 1) {                                         system.out.print("enter student id:");                     string id = scan.next();                     list.add(id);                     system.out.print("enter first name:");                     string name = scan.next();                     list.add(name);                     system.out.print("enter middle name:");                     string middle_name = scan.next();                     list.add(middle_name);                     system.out.print("enter last name:");                     string last_name = scan.next();                     list.add(last_name);                                system.out.println("do want query?(yes/no)");                     chooseyesorno = scan.next();                                              } else if (choosequery == 2) { //delete query                     system.out.print("enter student id:");                     string find_id = scan.next();                                                } else if(choosequery == 3) { //search query                  } else if(choosequery == 4) { //show query                     (string s : list) {                         system.out.println(s);                     }                 }             }         } while(chooseyesorno.equals("yes"));          system.out.println("do want @ tables?(yes/no)");         chooseyesorno2 = scan.next();                                        } while(chooseyesorno2.equals("yes"));      system.out.println("-----=====program terminated=====-----"); } 

  1. create student object contains fields need (student id, name, etc)

    class student {   int studentid;   string firstname;   string middlename;   string lastname; } 
  2. have 1 array list student objects

    java.util.list<student> list = new java.util.arraylist<student>(); 
  3. when delete operation selected, iterate through list find object , remove it. here's nice blog ways iterate through arraylist. favorite method follows:

    for (student std:list) {   if (std.studentid == targetid) {     list.remove(std);     break; //since you've removed target, can exit loop   } } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -