I am writing a series of classes that use inheritance. All of them compile except this one, called Processor. I am getting an "identifier expected" error at compile time at line 12, which is "public static void Processor(HouseholdArray[])" and at line 25, which is "public void processNextHousehold(hh)" Here is the code... /** * * Andy Moon * @version 1.00 10/31/07 */ public class Processor { private float avgAnuHHIncome; private int percentHHBlwPL; private int numOfHouseHolds; public static void Processor(HouseholdArray[]) { avgAnuHHIncome=0; percentHHBlwPL=0; numOfHouseHolds = 0; for(x=0;x(less than)HouseholdData_2.getNumberOfHouseholds();x++) { Household hh = HouseholdArray[x]; processNextHousehold(hh); } } public void processNextHousehold(hh) { float gi = hh.getAnnualIncome(); int hm = hh.getHouseHoldMembers(); updateAvgAnuHHIncome(gi); updatePCHHBlow(hm,gi); } private void updateAvgAnuHHIncome(float HIncome) { avgAnuHHIncome = avgAnuHHIncome + HIncome; numOfHouseHolds++; } private void updatePCHHBlow(int members, float income) { if (income < 6500 + (750*(members-2))) percentHHBlwPL++; } public void printStatistics() { } }
You might need to put an identifier for HouseholdArray[], I think in your case it may be HouseholdData_2. Also, for hh, you need to include the datatype, which is Household in your case.
You need parameter type declaration. If HouseholdArray is a class, you need to put it as: public static void Processor(HouseholdArray[] anArray) If HouseholdArray is parameter name, it should look like: public static void Processor(ClassOfHouseHoldArray[] HouseholdArray) Same with processNextHousehold(hh), you need to declare the class of hh like this: public void processNextHousehold(ClassOfHH hh) Replace ClassOfXXX with your actual class names. Hope this helps,
Tidied up your code a bit, andymoon, and used the [ code ] tags. Code: /** * * Andy Moon * @version 1.00 10/31/07 */ public class Processor { private float avgAnuHHIncome; private int percentHHBlwPL; private int numOfHouseHolds; public static void Processor(HouseholdArray[]) { avgAnuHHIncome=0; percentHHBlwPL=0; numOfHouseHolds = 0; for(x=0;x(less than)HouseholdData_2.getNumberOfHouseholds();x++){ Household hh = HouseholdArray[x]; processNextHousehold(hh); } } public void processNextHousehold(hh){ float gi = hh.getAnnualIncome(); int hm = hh.getHouseHoldMembers(); updateAvgAnuHHIncome(gi); updatePCHHBlow(hm,gi); } private void updateAvgAnuHHIncome(float HIncome){ avgAnuHHIncome = avgAnuHHIncome + HIncome; numOfHouseHolds++; } private void updatePCHHBlow(int members, float income){ if (income < 6500 + (750*(members-2))) percentHHBlwPL++; } public void printStatistics() { } } Arghhh... I caught the HouseHoldArray thingie, but I didn't reply until now.
Now, when I try to call my newly compiled Processor class,the compiler throws up at the code (in a class calld Driver)... public void processPoverty() { Processor p = new Processor(Household[] HouseholdArray, int numOfHouseholds); }
I tried both of these for the constructor... public void Processor(Household[] HouseholdArray, int nHouseholds) public void main(Household[] HouseholdArray, int nHouseholds)
New code...found out I have to pass a Household object to the Processor's processNextHousehold() method. Code: public void processPoverty() { for (x=0;x(less than)numOfHouseholds;x++) { Processor.processNextHousehold(Household HouseholdArray[x]); } }
This definitely won't work. You need to do something more like this: Code: public void processPoverty() { // i just arbitrarily picked 7 int numOfHouseholds = 7; Household[] householdArray = new Household[5]; for (int i=0; i < 5; i++) { // i didn't actually read your code, so i don't know what kind of constructor you need to use for Household householdArray[i] = new Household(); } Processor p = new Processor(householdArray, numOfHouseholds); }
Here is Driver in its entirety... Code: /** * * Andy Moon * @version 1.00 10/31/07 */ import javax.swing.JOptionPane; public class Driver { int numOfHouseholds=0; int x = 0; public void main(String[] args) { buildMenu(); HouseHoldData_2 hh2 = new HouseHoldData_2(); int numOfHouseholds = hh2.getNumberOfHouseholds(); Household HouseholdArray [] = new Household[(numOfHouseholds-1)]; while (hh2.hasMoreRecords()) { HouseholdArray[x] = new Household(hh2.getNextHouseholdId(),hh2.getNextHouseHoldAddress(),hh2.getNextHouseHoldPropertyTax(),hh2.getNextHouseHoldAnnualHazardInsurance(),hh2.getNextHouseHoldAnnualSchoolTax(),hh2.getNextAnnualIncome(),hh2.getNextNumberoFHouseholdMembers()); x++; } } public void buildMenu() { String x = JOptionPane.showInputDialog("Household Data Menu/n/n1. Inquire Household Information/n2. Process Poverty Information/n3. Exit."); int menu = Integer.parseInt(x); switch (menu) { case 1: inquireHousehold();break; case 2: processPoverty();break; case 3: break; default: buildMenu(); } } public void inquireHousehold() { String y = JOptionPane.showInputDialog("Please enter a household ID."); for (x=0;x(less than)numOfHouseholds;x++) { if } } public void processPoverty() { for (x=0;x(less than)numOfHouseholds;x++) { Processor.processNextHousehold(Household HouseholdArray[x]); } } }
Haven't written all that code. I have to iterate through the HouseholdArray and look for the Household ID, then display the string that I generate in each of the various toString methods.
Here is the code for that method... Code: if (y = HouseholdArray[x].getIdNumber()) { JOptionPane.ShowMessageDialog(Household.toString()); }
Like DarkHorse said, HouseholdArray is not a global variable, therefore is out of the scope of your processPoverty method. You have to pass it as a parameter or declare it as a global variable for your Driver class. p.s Does anyone know how much a Java programmer makes these days? I have 2 internship offer right now. One is working with C#, which I have never used before, but since it's so similar to Java, I think I can pick it up in no time. The other one is working with Java. Both are with very good fortune 500 companies. I'm just wondering which one is better for my future.
The Driver class creates the array in the first place (on the fourth line after the main declaration). How can it not be available?