Home Ask Login Register

Developers Planet

Your answer is one click away!

xdiver February 2016

How to insert and display arraylist in java program?

This is the output of the program:

Enter your last name: Dela Cruz
Enter your first name: Juan
Enter your course: BSCS
Enter your year: 4th Year

Display Array List

[0] - Dela Cruz | Juan | BSCS | 4th Year

Add More (y/n): if yes it will add more entry...

Answers


Rohan Kushwaha February 2016

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 your last name:");
            student.lastName = scn.nextLine();
            System.out.println("Enter your first name:");
            student.firstName = scn.nextLine();
            System.out.println("Enter your course:");
            student.course = scn.nextLine();
            System.out.println("Enter your year:");
            student.year = scn.nextLine();

            studentList.add(student);

            System.out.println("Add More (y/n): ");
            continueAdd = scn.nextLine();
        }
        int i = 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;

    }

}


anaxin February 2016

It would probably be better to have a class and work with objects but here is with List.

boolean flag = true;
List<String> list = new ArrayList<>();
int i = 0;
while (flag) {

    String lastname, firstname, code, year;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter lastname");
    lastname = sc.nextLine();
    System.out.println("Enter firstname");
    firstname = sc.nextLine();
    System.out.println("Enter code");
    code = sc.nextLine();
    System.out.println("Enter year");
    year = sc.nextLine();
    list.add(i, lastname + " | " + firstname + " | " + code + " | " + year);
    System.out.println(list.get(i));
i++;
    System.out.println("add  more? y/n");
    if (!"y".equals(sc.next())) {
        flag = false;
    }
}


Claudiu-Florin Stroe February 2016

You need :

public class Entries {
    private String lname;
    private String fname;
    private String course;
    private String year;

    public String getLname() {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }
    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public String getCourse() {
        return course;
    }
    public void setCourse(String course) {
        this.course = course;
    }
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }


}

and then your mainApp :

public class MainApp {
public static void main(String[] args) {
    List<Entries> students = new ArrayList<Entries>();
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a student:(y/n)");
    String option = scan.nextLine();
    while(option.equalsIgnoreCase("y")||option.equalsIgnoreCase("yes")){
        Entries stud = new Entries();
        System.out.println("Enter student's first name");
        stud.setFname(scan.nextLine());
        System.out.println("Enter student's last name");
        stud.setLname(scan.nextLine());
        System.out.println("Enter student's course");
        stud.setCourse(scan.nextLine());
        System.out.println("Enter student's year");
        stud.setYear(scan.nextLine());
        students.add(stud);
        System.out.println("Enter another student:(y/n)");
        option = scan.nextLine();
    } 
    if(option.equalsIgnoreCase("n")||option.equalsIgnoreCase("no")){
        System.out.println("Do you want to dispay all students? (y/n");
        String display = scan.nextLine();
        if(display.equalsIgnoreCase("y")||display.equalsIgnoreCase("yes"))
        for (Entries entrie : students) {
            System.out.println(entrie.getLname()+" |"+
          


KVK February 2016

use this code

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class hi {
    static String n,c,y;
    static String option="";
    static ArrayList al= new ArrayList();
    static Scanner s=new Scanner(System.in);
    public static void input(){


        System.out.println("Enter the name");
        n=s.next();
        System.out.println("Enter the course");
        c=s.next();
        System.out.println("Enter the year");
        y=s.next();



         al.add(n+"|"+c+"|"+y);



         System.out.println("Add More (y/n)");
            option=s.next();
            System.out.println(option);

            if(option.equals("y")|| option.equals("Y")){
                input();
            }
            else{

                System.out.println(al);
                //System.exit(0);
            }



        }

public static void main(String args[]) throws InstantiationException, IllegalAccessException{


    input();



}
}

Post Status

Asked in February 2016
Viewed 1,023 times
Voted 13
Answered 4 times

Search




Leave an answer


Quote of the day: live life