arrays - How to insert and display arraylist in java program? -
this output of program:
enter last name: dela cruz enter first name: juan enter course: bscs enter year: 4th year
display array list
[0] - dela cruz | juan | bscs | 4th year add more (y/n): if yes add more entry...
use following code.
import java.util.arraylist; import java.util.list; import java.util.scanner; public class io { public static void main(string[] args) { scanner scn = new scanner(system.in); string continueadd = "y"; list<student> studentlist = new arraylist<>(); student student; while ("y".equalsignorecase(continueadd)) { student = new student(); system.out.println("enter last name:"); student.lastname = scn.nextline(); system.out.println("enter first name:"); student.firstname = scn.nextline(); system.out.println("enter course:"); student.course = scn.nextline(); system.out.println("enter year:"); student.year = scn.nextline(); studentlist.add(student); system.out.println("add more (y/n): "); continueadd = scn.nextline(); } int = 0; for(student studenttemp : studentlist){ system.out.println("["+i+"] - " + studenttemp); i++; } } } class student { string firstname; string lastname; string course; string year; @override public string tostring(){ return lastname+" | "+ firstname +" | "+course+" | "+year; } }
Comments
Post a Comment