Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't Get Array Working

    I am having great difficulty implementing an arraylist in my code. I am trying to add four values that should be in my arraylist and then return that value. I can't even try to compile my code though, as there are too many errors. Can somebody please help me, I am really lost.
    Code:
    import java.util.ArrayList;
    
    public class Team {
    
    	public void T(Earnings teammate) {
    		
    		ArrayList teammate = new ArrayList();
    		
    		teammate.add(0,"chris");
    		teammate.add(1,"steve");
    		teammate.add(2,"mark");
    		teammate.add(3,"tom");
    		
    		
    		
    	}
    
    	public double totalGoals() {
    		ArrayList Employee = new ArrayList();
    		
    		double sum = 0;
    		for(int i = 0; i < teammate.size(); i++){
    			
    			Object F = teammate.get(i);
    			goal E = (goal) F;
    			
    			sum += team(i);
    			
    		}
    	
    		return sum;
    	}

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This is a problem with your scope. teammate is not available outside of the function T. You need to either return ArrayList from T and provide it as an argument for totalGoals, or you need to write it as a member for the class.
    I would recommend you do not use T as a name, especially when generics could be involved. ArrayList is generic, and will generate a warning when not provided with a datatype to work with. ArrayList should be generated under the type ArrayList<String>. Since I don't know what 'goal' is, its highly probable that you cannot cast a type of String to goal.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •