| | |
sorting through an array of objects
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2005
Posts: 3
Reputation:
Solved Threads: 0
Hi...I have created a private static main method which instantiates an array of 3 different types of objects depending on the choices entered in on the command line. Here is my code:
public static void main(String[] args)
{
//creates local variable currentIndex
//creates employee array of type worker which can hold up to 100 emloyee objects
Worker[] employee = new Worker[100];
//do/while loop
int i = 0;
do{
//if "w" for "worker" is entered, call Worker Class constructor, store
//pointer to worker name in employee array
if (args[i].equalsIgnoreCase("w"))
{
//store worker name in employee array
employee[currentIndex] = new Worker(args[i+1]);
//increment currentIndex counter by 1
currentIndex++;
//increment counter i by 2
i+=2;
}
//if "p" for "producer" is entered, call Producer Class constructor, store
//pointer to producer name and prod. target in employee array
else if (args[i].equalsIgnoreCase("p"))
{
//store producer name in next avail array slot, morph string value for
//production target and store as integer in next array slot
employee[currentIndex] = new Producer(args[i+1], Integer.parseInt(args[i+2]));
//increment currentIndex counter by 1
currentIndex++;
//increment counter i by 2
i+=3;
}
//if "m" for "manager" is entered, call Manager Class constructor, store
//pointer to manager name, number of subordinates, and whether or not this
//manager is liked in next array slot
else if (args[i].equalsIgnoreCase("m")){
//store manager name in next avail array slot, morph string value for
//number of subordinates and store as integer in next array slot along with
//liked/disliked result in following slot
employee[currentIndex] = new Manager(args[i+1], Integer.
parseInt(args[i+2]), args[i+3]);
//increment currentIndex counter by 1
currentIndex++;
//increment counter i by 2
i+=4;
}
else
{
//if choice was entered other than m,p,w, print error message
System.out.println("Must be M, P, or W");
//exit program
System.exit(1);
}
//end while loop
}while(i <args.length);
}
}//end class
I am supposed to create a private static method which is called by this main class once the array has been instantiated and called if the "Producer" class has an integer value greater than 50 (this is the parseInt(args[i+2]) in the Producer constructor call).
I can't quite get it to work.
Am trying to use this code:
for (int j =0; j < employee.length; j++){
if (employee[j].equals("Producer"))
{
System.out.println(Producer(args[j+1], (args[j +1], Integer.parseIntargs[j+2]));
}
}
I keep getting an error - cannot resolve method Producer(java.lang.string, int)
any help?
thanks.
public static void main(String[] args)
{
//creates local variable currentIndex
//creates employee array of type worker which can hold up to 100 emloyee objects
Worker[] employee = new Worker[100];
//do/while loop
int i = 0;
do{
//if "w" for "worker" is entered, call Worker Class constructor, store
//pointer to worker name in employee array
if (args[i].equalsIgnoreCase("w"))
{
//store worker name in employee array
employee[currentIndex] = new Worker(args[i+1]);
//increment currentIndex counter by 1
currentIndex++;
//increment counter i by 2
i+=2;
}
//if "p" for "producer" is entered, call Producer Class constructor, store
//pointer to producer name and prod. target in employee array
else if (args[i].equalsIgnoreCase("p"))
{
//store producer name in next avail array slot, morph string value for
//production target and store as integer in next array slot
employee[currentIndex] = new Producer(args[i+1], Integer.parseInt(args[i+2]));
//increment currentIndex counter by 1
currentIndex++;
//increment counter i by 2
i+=3;
}
//if "m" for "manager" is entered, call Manager Class constructor, store
//pointer to manager name, number of subordinates, and whether or not this
//manager is liked in next array slot
else if (args[i].equalsIgnoreCase("m")){
//store manager name in next avail array slot, morph string value for
//number of subordinates and store as integer in next array slot along with
//liked/disliked result in following slot
employee[currentIndex] = new Manager(args[i+1], Integer.
parseInt(args[i+2]), args[i+3]);
//increment currentIndex counter by 1
currentIndex++;
//increment counter i by 2
i+=4;
}
else
{
//if choice was entered other than m,p,w, print error message
System.out.println("Must be M, P, or W");
//exit program
System.exit(1);
}
//end while loop
}while(i <args.length);
}
}//end class
I am supposed to create a private static method which is called by this main class once the array has been instantiated and called if the "Producer" class has an integer value greater than 50 (this is the parseInt(args[i+2]) in the Producer constructor call).
I can't quite get it to work.
Am trying to use this code:
for (int j =0; j < employee.length; j++){
if (employee[j].equals("Producer"))
{
System.out.println(Producer(args[j+1], (args[j +1], Integer.parseIntargs[j+2]));
}
}
I keep getting an error - cannot resolve method Producer(java.lang.string, int)
any help?
thanks.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
If producer is static, then you shouldn't be calling it using the new keyword. It should be, ClassName.methodName().
I would have thought that would cause a "non static method being called from a static context" error though. Since that wasn't your error, then it could be that your method signature isn't correct, or that the parameter list isn't correct.
I would have thought that would cause a "non static method being called from a static context" error though. Since that wasn't your error, then it could be that your method signature isn't correct, or that the parameter list isn't correct.
![]() |
Similar Threads
- Dynamic memory for an array of objects (C++)
- Sorting 2D Dynamic array of integers , Snake Style (C)
- single array objects (Java)
- Help : Pointers to array of class objects . (C++)
Other Threads in the Java Forum
- Previous Thread: Multiple Classes
- Next Thread: Importance of checking the current thread?
| Thread Tools | Search this Thread |
911 addball android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class classes client code collision component crashcourse css database eclipse ee error event exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javadoc javafx javaprojects jni jpanel julia jvm key linux list loan loop machine map method methods migrate mobile netbeans newbie nls oracle output phone physics print problem program programming project radio recursion scanner screen server service set size sms socket software sort sql string swing textfield threads time transfer tree trolltech ubuntu utility windows






