1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

More help with Java...

Discussion in 'BBS Hangout' started by GladiatoRowdy, Nov 5, 2007.

  1. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    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()
    {

    }
    }
     
    #1 GladiatoRowdy, Nov 5, 2007
    Last edited: Nov 5, 2007
  2. OldManBernie

    OldManBernie Old Fogey

    Joined:
    May 5, 2000
    Messages:
    2,851
    Likes Received:
    221
    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.
     
  3. ynote

    ynote Member

    Joined:
    Nov 23, 2002
    Messages:
    32
    Likes Received:
    0
    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,

     
  4. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    Thanks, y'all.
     
  5. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    Tidied up your code a bit, andymoon, and used the [ code ] tags. :eek:
    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. :)
     
  6. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    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);
    }
     
  7. OldManBernie

    OldManBernie Old Fogey

    Joined:
    May 5, 2000
    Messages:
    2,851
    Likes Received:
    221
    did you write a constructor that included an both the Household[] and an int?
     
  8. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    I tried both of these for the constructor...

    public void Processor(Household[] HouseholdArray, int nHouseholds)

    public void main(Household[] HouseholdArray, int nHouseholds)
     
  9. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    Didn't know about the code tags...thanks.

    And, the code looks just that same way in JCreator.
     
  10. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    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]);
    	      }
    				
    	}
     
  11. The_Yoyo

    The_Yoyo Member

    Joined:
    Dec 25, 2001
    Messages:
    16,683
    Likes Received:
    2,873
    stands up and points at all the nerds! :D


    yeah sorry but i am of no java help
     
  12. DarkHorse

    DarkHorse Member

    Joined:
    Oct 9, 1999
    Messages:
    6,752
    Likes Received:
    1,296
    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);
    }
    
     
  13. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    I prefer the term "geek," thank you.
     
  14. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    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]);
    				}
    				
    			}
    
     }
    
     
  15. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    inside the inquireHousehold function, the "for" loop is open. Are we missing some code in the end? :confused:
     
  16. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    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.
     
  17. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    Here is the code for that method...

    Code:
    if (y = HouseholdArray[x].getIdNumber())
    	{
    	JOptionPane.ShowMessageDialog(Household.toString());
    	}
    
     
  18. The_Yoyo

    The_Yoyo Member

    Joined:
    Dec 25, 2001
    Messages:
    16,683
    Likes Received:
    2,873
    On a side note i missed ya on saturday andy

    next time though i'll get at ya
     
  19. LegendZ3

    LegendZ3 Member

    Joined:
    Nov 6, 2002
    Messages:
    4,196
    Likes Received:
    5
    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.
     
    #19 LegendZ3, Nov 5, 2007
    Last edited: Nov 5, 2007
  20. GladiatoRowdy

    GladiatoRowdy Member

    Joined:
    Oct 15, 2002
    Messages:
    16,596
    Likes Received:
    496
    The Driver class creates the array in the first place (on the fourth line after the main declaration). How can it not be available?
     

Share This Page