java - Why is that when I search and write the desired index, it displays all the student list? -
this code. after adding 2 or more students in list , press search , desired index, shows student list need 1 output not students i've input.
studentserviceimpl studservimpl = new studentserviceimpl(); scanner scan = new scanner(system.in); do{ system.out.print("select number: "); system.out.println(); number=scan.nextint(); list<student> studentlist = new arraylist<student>();
this add multiple student list
if(number==1){ student student = new student(); system.out.println("enter student no."); studid = scan.next(); student.setstudid(studid); system.out.println("enter first name:"); firstname = scan.next(); student.setfirstname(firstname); system.out.println("enter last name:"); lastname = scan.next(); student.setlastname(lastname); studservimpl.add(student); }
this search method
else if(number ==2) { studentlist = studservimpl.search(index); system.out.println("enter desired index"); index = scan.nextint(); (student student2 : studentlist) { system.out.println("student id: "+ student2.getstudid()); system.out.println("firstname: "+student2.getfirstname()); system.out.println("lastname: "+student2.getlastname()+"\n"); } }
this exit...
else if(number == 3){ system.out.println("thank you, bye!"); } } while(number!=0); } }
this search method class, studentserviceimpl:
public list<student> search(int index) { studentlist.get(index); return studentlist; }
Comments
Post a Comment