mismatch error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandyw
    New Member
    • Mar 2007
    • 122

    mismatch error

    Help please
    I get this error at bottom of the program for (EmployeeClass idx : arlist)
    I get the error on arlist
    Type mismatch: cannot convert from element type Object to EmployeeClass


    How do I solve this error.
    Here is part of my code:

    Code:
    public class Employee_listing {
        static ArrayList arlist;
        static Scanner kbd;
    
        public static EmployeeClass makePerson() {
        	EmployeeClass temp = null;
    
            // prompt for data
        	String id;
        	String ln;
            String fn;
    
            System.out.print("Enter Employee ID ==>");
            id = kbd.next();
    
            System.out.print("Enter Last Name ==>");
            ln = kbd.next();
    
            System.out.print("Enter First Name ==>");
            fn = kbd.next();
    
    
            // make an object
            temp = new EmployeeClass(id,ln,fn);
    
            return temp;
        }//close the public static EmployeeClass 
    
        public static void main(String[] args) {
            // make array list object
            arlist = new ArrayList();
    
            // make a scanner
            kbd = new Scanner(System.in);
    
            // create people until select stop
            boolean endData = false;
    
            while (!endData) {
            	EmployeeClass temp = makePerson();
                arlist.add(temp);
                System.out.println("More (Y/N)-->");
    
                String ans = kbd.next();
    
                if (ans.equalsIgnoreCase("N")) {
                    endData = true;
                }//close the if loop
            }//close the while loop
    
            // print out all elements of array list
            for (EmployeeClass idx : arlist) {
    System.out.printf("Name is %s - %s \n", idx.getFname(),idx.getLname());
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sandyw
    Help please
    I get this error at bottom of the program for (EmployeeClass idx : arlist)
    I get the error on arlist
    Type mismatch: cannot convert from element type Object to EmployeeClass


    How do I solve this error.
    Here is part of my code:

    Code:
    public class Employee_listing {
    static ArrayList arlist;
    static Scanner kbd;
     
    public static EmployeeClass makePerson() {
    	EmployeeClass temp = null;
     
    // prompt for data
    	String id;
    	String ln;
    String fn;
     
    System.out.print("Enter Employee ID ==>");
    id = kbd.next();
     
    System.out.print("Enter Last Name ==>");
    ln = kbd.next();
     
    System.out.print("Enter First Name ==>");
    fn = kbd.next();
     
     
    // make an object
    temp = new EmployeeClass(id,ln,fn);
     
    return temp;
    }//close the public static EmployeeClass 
     
    public static void main(String[] args) {
    // make array list object
    arlist = new ArrayList();
     
    // make a scanner
    kbd = new Scanner(System.in);
     
    // create people until select stop
    boolean endData = false;
     
    while (!endData) {
    	EmployeeClass temp = makePerson();
    arlist.add(temp);
    System.out.println("More (Y/N)-->");
     
    String ans = kbd.next();
     
    if (ans.equalsIgnoreCase("N")) {
    endData = true;
    }//close the if loop
    }//close the while loop
     
    // print out all elements of array list
    for (EmployeeClass idx : arlist) {
    System.out.printf("Name is %s - %s \n", idx.getFname(),idx.getLname());
    }
    }
    Define your ArrayList to take objects of type EmployeeClass.

    Code:
     
    ArrayList<EmployeeClass > list = new ArrayList<EmployeeClass >();

    Comment

    • sandyw
      New Member
      • Mar 2007
      • 122

      #3
      once again thanks for the help

      Comment

      Working...