简体   繁体   中英

Console not printing anything

It is not printing anything there is no error. kindly help me in resolving his error. I have tried three methods to print but still not a single one is working and there isn't any error.

import java.util.ArrayList;

public class JavaEight {

    public static void main(String args[])
    {
        ArrayList<Employee> Emp = new ArrayList<>();
        Employee e1 = new Employee(101, "Ravi", "Delhi", "2000");
        Employee e2 = new Employee(102, "Vineet", "Mangalore", "5000");
        Employee e3 = new Employee(103, "Punit", "Mumbai", "3000");
        Employee e4 = new Employee(104, "Shruti", "Banglore", "6000");
        Employee e5 = new Employee(105, "Ritu", "Hyderabad", "8000");
        
        for(int i=0; i<Emp.size();i++)
        {   
            Employee e = (Employee)Emp.get(i);
            System.out.println(e);
            
        }
        Emp.forEach(i -> System.out.println(Emp));
        
        for(Employee i : Emp)
        {
            System.out.println(Emp);
        }
        
        
    }
    }

emp.add(e1); etc is missing

you have created the employee objects, but you did not add them to the list. thus the list is blank, and not printing anything

add Employee in your ArrayList.

     ArrayList<Employee> Emp = new ArrayList<>();
        Employee e1 = new Employee(101, "Ravi", "Delhi", "2000");
        Employee e2 = new Employee(102, "Vineet", "Mangalore", "5000");
        Employee e3 = new Employee(103, "Punit", "Mumbai", "3000");
        Employee e4 = new Employee(104, "Shruti", "Banglore", "6000");
        Employee e5 = new Employee(105, "Ritu", "Hyderabad", "8000");
        Emp.add(e1);
        Emp.add(e2);
        Emp.add(e3);
        Emp.add(e4);
        Emp.add(e5);

hope , it will resolve your issue.

This is because your ArrayList Emp is empty. You have created the Employee object but you have not added it to Emp. add it to Emp like Emp.add(e1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM