8 Replies - 8003 Views - Last Post: 09 May 2010 - 11:09 AM Rate Topic: -----

#1 mega105wr   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 08-May 10

Cannot Find Variable length?

Posted 09 May 2010 - 10:22 AM

Quote

My code is giving me trouble......its telling it cant find variable length


 public void addTeam()
    {
        System.out.print("\nWould you like to add the teams to group 1 or 2: ");
        int selection = scan.nextInt(); 
        System.out.println("");
        
        for(int z = 1; z <= 4; z++)
        {
        System.out.print("Enter the name of the team: ");
        String name = scan.next();
        System.out.print("Enter the location of the team: ");
        String location = scan.next();
        System.out.println("");
        
        Team team = new Team(teamLocation, teamName);
        if(selection == 1){groupA.addTeam();}
           else 
           
           {groupB.addTeam();}        
        
        }
    
    for(int index = 0; index < groupB.length; index++)
    {
        boolean addTeamtoGroup = false;
        if(group[index] == null && addTeamtoGroup == false)
        {
            group[index] = team;
            addTeamtoGroup = true;
            // break;
        }
    }
    return true;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot Find Variable length?

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Cannot Find Variable length?

Posted 09 May 2010 - 10:23 AM

Where is the variable groupB declared? I'm guessing that since it isn't an array, that's your problem. Note that arrays have a variable length, Strings have a method length(), and Lists have a method size(), all of which are comparable.
Was This Post Helpful? 0
  • +
  • -

#3 mega105wr   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 08-May 10

Re: Cannot Find Variable length?

Posted 09 May 2010 - 10:28 AM

Ive Declared it in the same class as this code is in....

I've declared it as
public static Group groupB = new Group();
.

so what should I do?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Cannot Find Variable length?

Posted 09 May 2010 - 10:30 AM

It sounds like the Group class doesn't have a length variable. Does it contain a List, array, or String that you could access a corresponding attribute for?
Was This Post Helpful? 0
  • +
  • -

#5 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Cannot Find Variable length?

Posted 09 May 2010 - 10:32 AM

Well it could be helpfull if you posted the Group class, it's members, variables, to get a clear view of what you try to do.
especially, did you create a method to retrieve the current length\size of a Group Object?
Was This Post Helpful? 0
  • +
  • -

#6 mega105wr   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 08-May 10

Re: Cannot Find Variable length?

Posted 09 May 2010 - 10:40 AM

Quote

Here it is


import java.util.Scanner;
import java.util.Arrays;


public class Group
{
    //Team Variables
    public static Team team;
    private String teamLocation;
    private String teamName;
    public static int matchesPlayed;
    public static int matchPoints;
    public static int TotalGoalsFor; 
    public static int TotalGoalsAgainst;
    static Scanner scan = new Scanner(System.in);
    public static Group groupA = new Group();
    public static Group groupB = new Group();
    
    
    
    public static void main(String args[])
    {
        int[] groupA = new int[4];
        int[] groupB = new int[4];
        
        boolean contentsEquvilant = true;
        for (int i =0; i < 4; i++)
        {
            if(groupA[i]!= groupB[i])
            { contentsEquvilant = false;  }
        }
        if (contentsEquvilant==true)
        {
            System.out.println("Team has already been selected");
        }
    }
    
    public static void objectTeam()
    {
        final int MAX = 4;
        String[] teamName = new String[MAX];
        
        teamName[0] = new String("South Africa");
        teamName[1] = new String("Australia");
        teamName[2] = new String("South Korea");
        teamName[3] = new String("All whites");
        
        for (int i=0;i< MAX; i++)
        {
            System.out.println(" -"+ teamName[i]);
        }
    }

    public void addTeam()
    {
        System.out.print("\nWould you like to add the teams to group 1 or 2: ");
        int selection = scan.nextInt(); 
        System.out.println("");
        
        for(int z = 1; z <= 4; z++)
        {
        System.out.print("Enter the name of the team: ");
        String name = scan.next();
        System.out.print("Enter the location of the team: ");
        String location = scan.next();
        System.out.println("");
        
        Team team = new Team(teamLocation, teamName);
        if(selection == 1){groupA.addTeam();}
           else 
           
           {groupB.addTeam();}        
        
        }
    
    for(int index = 0; index < Group.length; index++)
    {
        boolean addTeamtoGroup = false;
        if(group[index] == null && addTeamtoGroup == false)
        {
            group[index] = team;
            addTeamtoGroup = true;
            // break;
        }
    }
    return true;
}
        
          
    //Accessor 
    public String getTeamLocation (String teamLocation)
    {
        return teamLocation;
    }
    public String getTestTeam (String testTeam)
    {
        return testTeam;
    }
     public String getTeamName(String teamName)
     {
        return teamName;  
    }
     public int getMatchPoints ()
     {
        return matchPoints;
    }
     public int getTotalGoalsFor ()
     {
        return TotalGoalsFor;
    }
    public int getTotalGoalsAgainst ()
    {
        return TotalGoalsAgainst;
    }
    public int getMatchesPlayed ()
    {
        return matchesPlayed;
    }
    
    
      //Set methods
     public boolean setTeamLocation()
     {

        return true;
     }
     public boolean setTeamName()
     {
        
        return true;
     }
     public boolean setTestTeam()
     {
         return true;
        }
     public void setMatchPoints()
     {
        this.matchPoints=0;
     }       
     public void setTotalGoalsFor()
     {
        this.TotalGoalsFor=0;
     }
     public void setTotalGoalsAgainst()
     {
         this.TotalGoalsAgainst=0;
     }
     public void setMatchesPlayed()
     {
         this.matchesPlayed=0;
     }
   
     
     //Increment Matches Played
      public int incrementMatchesPlayed(int matchesPlayed)
      {
        this.matchesPlayed=matchesPlayed + matchesPlayed;
        return this.matchesPlayed;
      }
      
      //Increment Matches Points
      public int incrementMatchPoints(int matchPoints)
      {
          this.matchPoints=matchPoints + matchPoints;
          return this.matchPoints;
      }
       //Increment Goals For
       public int incrementGoalsFor (int ToatalGoalsFor)
       {
           this.TotalGoalsFor=TotalGoalsFor + TotalGoalsFor;
           return this.TotalGoalsFor;
       }
        //Increment Goals Againts
        public int incrementGoalsAgainst(int TotalGoalsAgainst)
        {
            this.TotalGoalsAgainst=TotalGoalsAgainst + TotalGoalsAgainst;
            return this.TotalGoalsAgainst;
        }
   
     
     //toString method
    public String toString()
    {
        return teamLocation + ", " + teamName + ", "+ matchPoints + ", "+ TotalGoalsFor + ", " + TotalGoalsAgainst + ", ";                     
    }


     
    }


        


Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Cannot Find Variable length?

Posted 09 May 2010 - 10:48 AM

Your Group class does not contain a variable length, and the arrays are local to the main() method. This isn't great class design, but I know I adressed this in your other thread as well as linked to tutorials illustrating good class design.
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Cannot Find Variable length?

Posted 09 May 2010 - 10:48 AM

You are trying to access Group.length which is not a static or local method or variable. In order to call that, you have to have a length attribute or a method to call.
Was This Post Helpful? 0
  • +
  • -

#9 mega105wr   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 08-May 10

Re: Cannot Find Variable length?

Posted 09 May 2010 - 11:09 AM

Thanks so Much guys.....i got it now...really appreciate it .

Quote

PEACE

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1