RpTheHotrod Posted February 22, 2011 Share Posted February 22, 2011 Unfortunately I do not have access to a compiler right now, and I'm not sure yet how arrays work with java as I have not had the time to research it and we have not covered java arrays yet. I'm trying to come up with a way that will allow multiple objects to be created via a loop with user input. Would something like this work, or will arrays not work with objects like this? I know the code is inefficient, but we're being asked to specifically do it this way. import javax.swing.JOptionPane; //Class public class Employee_Tester { //Main Statement public static void main(String[] args) { objArray = new int[11]; int x = 1; //Creating Obj1 (no parameters) Employee obj[x] = new Employee(); obj[x].setName("Susan Meyers"); obj[x].setDepartment("Accounting"); obj[x].setPosition("Vice President"); obj[x].setIdNumber(47899); x++; //Creating Obj2 (with paramaters) Employee obj[x] = new Employee("Mark Jones", "IT", "Programmer",39119); x++; //Creating obj3 (by use of copy contructor) Employee obj[x] = new Employee(new Employee("Joy Rogers", "Manufacturing", "Engineer",81774)); x++; //Windows output window JOptionPane.showMessageDialog(null, "Congratulations. You are successful to use object!"); char exit; do { //Creating object for upcoming user input Employee obj[x] = new Employee(); obj[x].setName(JOptionPane.showInputDialog("Please enter name.")); obj[x].setDepartment(JOptionPane.showInputDialog("Please enter the department.")); obj[x].setPosition(JOptionPane.showInputDialog("Please enter the position.")); String idNumStr = JOptionPane.showInputDialog("Please enter the id number."); int idNumResult = Integer.parseInt(idNumStr); obj[x].setIdNumber(idNumResult); x++; String _exit = JOptionPane.showInputDialog("Would you like to exit? Y/N"); exit = _exit.charAt(0); } while(!(exit == 'Y' || exit == 'y')); System.exit(0); } } Link to comment Share on other sites More sharing options...
Hallucination Posted February 22, 2011 Share Posted February 22, 2011 You'll want to start x at 0, since that is where Java arrays start. If you did the do while loop properly you shouldn't need the System.exit(0) line, it should just end on its own. Edit: You'll also want the array to be an array of Employees, not an array of integers. I'm a bit rusty with Java, but it should look something like: Employee objArray = new Employee[11]; That would also mean that you don't have to declare the elements of objArray, you just have to assign them. I.E. Employee obj[x] = new Employee(); would become: objArray[x] = new Employee(); And lastly, you need to change all instances of obj[x] to objArray[x], otherwise you're referencing an array that doesn't exist. I made all those changes, and the only error that is coming up is that I don't have an Employee class. Link to comment Share on other sites More sharing options...
RpTheHotrod Posted February 22, 2011 Author Share Posted February 22, 2011 You'll want to start x at 0, since that is where Java arrays start. If you did the do while loop properly you shouldn't need the System.exit(0) line, it should just end on its own. Edit: You'll also want the array to be an array of Employees, not an array of integers. I'm a bit rusty with Java, but it should look something like: Employee objArray = new Employee[11]; That would also mean that you don't have to declare the elements of objArray, you just have to assign them. I.E. Employee obj[x] = new Employee(); would become: objArray[x] = new Employee(); And lastly, you need to change all instances of obj[x] to objArray[x], otherwise you're referencing an array that doesn't exist. I made all those changes, and the only error that is coming up is that I don't have an Employee class. I agree but the instructor wants us to start with obj1. As for system exit we were instructed that when using the pane a system exit is mandatory. Odd. Anyway I received thus response from someone. You can declare an array of objects using: Employee[] obj = new Employee[10]; substituting 10 for however many Employee objects you want to create After declaring the array, you can assign each one a value by using obj[x] = new Employee(); You can't have a variable in the variable name at declaration. obj[x] doesn't turn into obj1 where you say "creating obj1," unfortunately. That objArray needs an int[] in front of it too. int[] objArray = new int[11]; Other than that it seems fine. Link to comment Share on other sites More sharing options...
RpTheHotrod Posted February 22, 2011 Author Share Posted February 22, 2011 You'll want to start x at 0, since that is where Java arrays start. If you did the do while loop properly you shouldn't need the System.exit(0) line, it should just end on its own. Edit: You'll also want the array to be an array of Employees, not an array of integers. I'm a bit rusty with Java, but it should look something like: Employee objArray = new Employee[11]; That would also mean that you don't have to declare the elements of objArray, you just have to assign them. I.E. Employee obj[x] = new Employee(); would become: objArray[x] = new Employee(); And lastly, you need to change all instances of obj[x] to objArray[x], otherwise you're referencing an array that doesn't exist. I made all those changes, and the only error that is coming up is that I don't have an Employee class. Just saw your edit So how's this: Here's the Employee Class //Class Header public class Employee { //data members private String name, department, position; private int idNumber; //default constructor public Employee() { name = ""; department = ""; position = ""; idNumber = 0; } //parameter constructor public Employee(String _name, String _department, String _position, int _idNumber) { name = _name; department = _department; position = _position; idNumber = _idNumber; } //copy constructor public Employee(Employee constructor) { name = constructor.name; department = constructor.department; position = constructor.position; idNumber = constructor.idNumber; } //Mutator Methods public void setName(String _name) { name = _name; } public void setDepartment(String _department) { department = _department; } public void setPosition(String _position) { position = _position; } public void setIdNumber(int _idNumber) { idNumber = _idNumber; } //Accessor methods public String getName() { return name; } public String getDepartment() { return department; } public String getPosition() { return position; } public int getIdNumber() { return idNumber; } public String toString() { String str = "Name: " + name + "\nDepartment: " + department + "\nPosition: " + position + "\nidNumber: " + idNumber; return str; } } Actual Program import javax.swing.JOptionPane; //Class public class Employee_Tester { //Main Statement public static void main(String[] args) { int x = 1; Employee[] obj = new Employee[11]; obj[x] = new Employee(); obj[x].setName("Susan Meyers"); obj[x].setDepartment("Accounting"); obj[x].setPosition("Vice President"); obj[x].setIdNumber(47899); x++; //Creating Obj2 (with paramaters) obj[x] = new Employee("Mark Jones", "IT", "Programmer",39119); x++; //Creating obj3 (by use of copy contructor) obj[x] = new Employee(new Employee("Joy Rogers", "Manufacturing", "Engineer",81774)); x++; //Windows output window JOptionPane.showMessageDialog(null, "Congratulations. You are successful to use object!"); char exit; do { //Creating object for upcoming user input obj[x] = new Employee(); obj[x].setName(JOptionPane.showInputDialog("Please enter name.")); obj[x].setDepartment(JOptionPane.showInputDialog("Please enter the department.")); obj[x].setPosition(JOptionPane.showInputDialog("Please enter the position.")); String idNumStr = JOptionPane.showInputDialog("Please enter the id number."); int idNumResult = Integer.parseInt(idNumStr); obj[x].setIdNumber(idNumResult); x++; String _exit = JOptionPane.showInputDialog("Would you like to exit? Y/N"); exit = _exit.charAt(0); } while(!(exit == 'Y' || exit == 'y')); System.exit(0); } } Link to comment Share on other sites More sharing options...
Hallucination Posted February 23, 2011 Share Posted February 23, 2011 Alright, just copypasta'd the employee class in, and my code is running fine. I did the same with yours, it's working. And ignore what I said about the System.exit bit, there's a reason I hate UI programming. Link to comment Share on other sites More sharing options...
RpTheHotrod Posted February 23, 2011 Author Share Posted February 23, 2011 I decided to try an input tester. The program seems to run fine and as intended, but for some reason I get an error that says "exception in thread main". I don't see any issues, though. The program runs as intended, there's no compiler errors. Everything seems normal. Any ideas? import javax.swing.JOptionPane; //Class public class Employee_Tester { //Main Statement public static void main(String[] args) { int x = 1; Employee[] obj = new Employee[11]; obj[x] = new Employee(); obj[x].setName("Susan Meyers"); obj[x].setDepartment("Accounting"); obj[x].setPosition("Vice President"); obj[x].setIdNumber(47899); x++; //Creating Obj2 (with paramaters) obj[x] = new Employee("Mark Jones", "IT", "Programmer",39119); x++; //Creating obj3 (by use of copy contructor) obj[x] = new Employee(new Employee("Joy Rogers", "Manufacturing", "Engineer",81774)); x++; //Windows output window JOptionPane.showMessageDialog(null, "Congratulations. You are successful to use object!"); char exit; do { //Creating object for upcoming user input obj[x] = new Employee(); obj[x].setName(JOptionPane.showInputDialog("Please enter name.")); obj[x].setDepartment(JOptionPane.showInputDialog("Please enter the department.")); obj[x].setPosition(JOptionPane.showInputDialog("Please enter the position.")); String idNumStr = JOptionPane.showInputDialog("Please enter the id number."); //Convert input String to Int int idNumResult = Integer.parseInt(idNumStr); obj[x].setIdNumber(idNumResult); x++; //Option to continue the loop or exit String _exit = JOptionPane.showInputDialog("Would you like to continue? Y/N"); exit = _exit.charAt(0); } while(exit == 'Y' || exit == 'y'); //Testing input for(int counter = 1; counter <= x; counter++) JOptionPane.showMessageDialog(null, obj[counter].toString()); System.exit(0); } } Link to comment Share on other sites More sharing options...
RpTheHotrod Posted February 24, 2011 Author Share Posted February 24, 2011 I fixed it. Silly me with a typo. //Testing input for(int counter = 1; counter < x; counter++) JOptionPane.showMessageDialog(null, obj[counter].toString()); System.exit(0); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.