ArrayList Array Type mismatch

478 views Asked by At

I'm trying to (for a school lab) create a program that will, for now, just display the items in an Array (We're using JGrasp as our IDE, this is a required part, as it requres the projects that it makes). It requires five classes: A superclass for Employee, two subclasses defining types of employees, a testing method (UseCompany), and a FileIO class.

import java.util.*;

public class UseCompany {
    public static void main(String[] args) {
        System.out.print("Hello");
        SalesMan man1 = new SalesMan("John Doe", 12345);
        Technician tech1 = new Technician("Jane Doe", 12346);
        ArrayList<Employee> company = new ArrayList<Employee>();
        company.add(man1);
        company.add(tech1);
        SalesMan man2 = new SalesMan(1500.00, "Steelport", "Johnny Gat", 14432);
        Technician tech2 = new Technician(5, "IT Support", "Kinzie Kensington", 10000);
        company.add(man2);
        company.add(tech2);
        Collections.sort(company);
        ArrayList<Employee> temp = company;
        FileIO.displayArray(temp); //Line 19, the problem point.
    }
}

This is the testing method, and this

public static void displayArray(ArrayList<Employee> list) //Displays array one line at a time
{                                                            //Specialized for Employee class
  for (int i=0;i<list.size();i++)
  {
     if((list.get(i)).getType() == 'T')
     {
        System.out.println("Name: " + (list.get(i)).getName() + "\tNo.: " + (list.get(i)).getNumber() + "\tType: Technician\tDepartment: " + ((Technician)(list.get(i))).getDepartment() + "\tClearance Level: " + ((Technician)(list.get(i))).getLevel());
     }
     else if((list.get(i)).getType() == 'S')
     {
        System.out.println("Name: " + (list.get(i)).getName() + "\tNo.: " + (list.get(i)).getNumber() + "\tType: Salesperson\tTerritory: " + ((SalesMan)(list.get(i))).getTerritory() + "\tTarget: " +((SalesMan)(list.get(i))).formattedTarget());
     }
  }
}

Is the method in FileIO that's supposed to display the lines.

The problem I'm having is that while it compiles fine, it gives the error

UseCompany.java:19: error: incompatible types: List cannot be converted to ArrayList[]

When I try to run it in jgrasp, pointing at (temp). My teacher and I both had a crack at it, and couldn't do anything with it (he added the temp stuff).

However, if I run it in command line using the bats I made for compiling and running, it runs just fine! Can anyone give any tips on why I'm getting this error in JGrasp, and perhaps how to fix it so that I can continue working on it?

/**
* This is the superclass Employee.
* It contains the general information for SalesMan and Technician
*/
import java.io.*;

public class Employee implements Comparable, Serializable
{
private String name;            // Name of employee
private int number;         // Employee number of employee
char typeCode;   // What kind of employee is it

/**
* Constructor  to create the superclass
* @param nom Name of the employee
* @param number Employee number
*/
public Employee(String nom, int number )
{
    name = nom;
    this.number = number;
}

public char getType()
{
   return typeCode;
}

public int getNumber()
{
   return number;
}

public String getName()
{
   return name;
}

/**
* compareTo method for comparison of objects
* @param Employee object
* @return An integer to indicate comparison  
*/
public int compareTo(Object o)
{
    //Compare code
    return name.compareTo(((Employee)o).name);
}

/** toString method for printing */
public String toString()
{
  String employeeNum = Integer.toString(number);
  String str = new String("Name: " + name + "\nEmployee Number: " + employeeNum);
    return str;
}
}

For information: Technician adds private int level, private string department, getters, and a toString Salesman adds private double target, private string territory, getters, and a toString

Edit: For some reason, even after removing all of the arguments from both the call to and the implementation of the displayArray method, and commenting out its entire contents, it seems to be requiring data of type ArrayList[], pointing the error at the dot between FileIO and displayArray()

FINAL EDIT: Thank you Fildor. As it would turn out, deleting the project file and remaking it was, in fact, the solution. Everything works fine now. I wish either me or my teacher had figured this out last night in lab...

2

There are 2 answers

0
Kaerea On BEST ANSWER

The answer to this question in particular was, in JGrasp, delete the associated .gpj file, then remake it using the same file. It should hold the updated versions of the files with no further errors. Thank you again, Fildor, for suggesting this answer.

0
Fildor On

Answer instead of comments:

It seems that jGrasp uses some outdated class files at runtime. Due to my lack of knowledge of that IDE, I cannot tell how, but I suggest a complete cleanup of the compiled classes.

I also suggest you write your own answer, describing what you did to make that error go away and accept that one.