问题
I am very very new to my intro to java course and I was looking for help with an error that I was receiving. The error message is posted below along with with the actual code. Does anyone know why I am receiving this message and anyway to help me? The code is able able to compile and run but instead of printing at the end I receive a pop up error message (screen shot below) but I don't understand what it means or why I am getting it. Can anyone help? Thanks!
public class Employee10
{
public static void main ( String args[] )
{
Employee e1 = new Employee();
Employee[] arr = new Employee[2];
int j = 0;
for ( int i=0; i < 3; i++)
{
arr[0] = e1;
String nameF = Input.getString("Please enter a First Name");
String nameL = Input.getString("Please enter a Last Name");
int Number = Input.getInt("Please enter an Employee Number");
String Street = Input.getString("Please enter a Street address");
String City = Input.getString("Please enter a City");
String State = Input.getString("Please enter a State");
double Zip = Input.getDouble("Please enter a Zip Code");
int Month = Input.getInt("Please enter a Month");
int Day = Input.getInt("Please enter a Day");
int Year = Input.getInt("Please enter a Year");
e1.setNumber(Number);
e1.setName( new Name(nameF, nameL));
e1.setAddress(new Address(Street, City, State, Zip));
e1.setHireDate(new Date(Month, Day, Year));
System.out.println(e1.getEmployeeString());
arr[i] = e1;
}
for ( j=0; j < arr.length; j++ )
{
System.out.println( arr[j].getEmployeeString() );
}
}
}
ERROR MESSAGE: ( Unfortunately I am not able to embed a photo so I just have to type out the code so here it is):
The Java class file "Employee10.class" could not be launched. Check the Console for possible error messages.
What does all of this mean? Where is the Console I can check?
回答1:
Try to do some thing like this:
Employee e2 = new Employee();
arr[j] = e2 ;
Because in arr
you can add only elements of type Employee
;
回答2:
It looks like you took the bad line out before posting your code. Are you trying to use that line to iterate through the for loop? If so, that's not needed. Try:
for ( j=0; j < arr.length; j++ )
{
System.out.println( arr[j].getEmployeeString() );
}
You'll also need to change arr[0] = e1; to arr[i] = e1; in your loop getting employee information. And you should create your e1 Employee object in that loop so that you get a new Employee each time.
回答3:
I completely understand what you are trying to do, You are trying to assign the next employee object to the current one, and the code you have written is syntactically wrong.
Here is the code that needs to be changed
arr[j] = ;
needs to be changed to
arr[j] = arr[j+1];
来源:https://stackoverflow.com/questions/34276009/error-message-int-cannt-be-converted-to-type