Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

a Java n00b I are - help please

  • 13-07-2015 7:30pm
    #1
    Registered Users, Registered Users 2 Posts: 19


    I'm 3 weeks into the OCJA course and for fear I've missed something really silly I'd like to ask here first if I may:

    In my main method I am trying to create 5 objects (Players).
    I am using a String array to hold their names.
    I wish to iterate through 5 times and on each iteration have the counter in the 'for loop' assign the name.

    //create 5 cpu players
    String[] cpuNames = {"A","B","C","D","E"};
    for(int i = 0;i <= 4;++i){
    String tmpName = cpuNames;

    Player tmpName= new Player();

    }
    eclipse is complaining that indeed there's a duplicate variable 'tmpName'

    This also won't work:

    //create 5 cpu players
    String[] cpuNames = {"A","B","C","D","E"};
    for(int i = 0;i <= 4;++i){
    Player cpuNames= new Player();

    }

    help please?


Comments

  • Registered Users, Registered Users 2 Posts: 1,275 ✭✭✭bpmurray


    Freqm0n wrote: »
    eclipse is complaining that indeed there's a duplicate variable 'tmpName'

    As indeed there is! You have
    String tmpName = cpuNames[i];
    
    and
    Player tmpName= new Player();
    
    As you can see, you have two variables called "tmpName", the first is a String and the second is a Player. The way I'd do it is (note the white space - it costs nothing to put in blank lines & spaces and makes it infinitely easier to read):
    static final int PLAYERCOUNT = 5; 
    
    //create 5 cpu players
    String[] cpuNames = {"A","B","C","D","E"};
    
    for (int i=0; i<PLAYERCOUNT; i++) { // NOT ++i since you'll miss the first one!
    
    // You don't need to copy the string to a variable!
    Player tmpPlayer = new Player(cpuNames[i]); 
    
    // Do whatever you have to with the new player ....
    
    }
    


  • Registered Users, Registered Users 2 Posts: 190 ✭✭NeutralHandle


    Freqm0n wrote: »
    This also won't work:

    //create 5 cpu players
    String[] cpuNames = {"A","B","C","D","E"};
    for(int i = 0;i <= 4;++i){
    Player cpuNames= new Player();

    }

    help please?

    cpuNames[] is an array of strings. You cannot initialise an element of it to be a Player object.
    You are also trying to declare elements of the array as Players in the last line, which doesn't make sense.
    You should use a post-increment for i also (i++), not a pre-increment (++i). Pre-increments increment the value before evaluating it. So in this case the firs iteration of the loop would be using the second element of the array instead of the first (index 1 not 0).


  • Registered Users, Registered Users 2 Posts: 19 Freqm0n


    //create 5 cpu players
    final int PLAYERCOUNT = 5;

    String[] cpuNames = {"A","B","C","D","E"};

    for(int i=0;i<PLAYERCOUNT;i++){

    Player tmpPlayer = new Player(cpuNames); //error here


    }

    it's not allowing me to create a static final - is that the reason I have a "the constructor Player(String) is undefined" error now?

    ...we have not covered constructors yet but have covered renaming of the object reference variable...could I not simply create the object as normal(Player tmpName = new Player;) first on each iteration and next line would simply rename the reference from and element of String from the array?


  • Registered Users, Registered Users 2 Posts: 19 Freqm0n


    //create 5 cpu players
    final int PLAYERCOUNT = 5;

    String[] cpuNames = {"A","B","C","D","E"};

    for(int i=0;i<PLAYERCOUNT;i++){

    Player tmpPlayer = new Player(cpuNames); //error here


    }

    it's not allowing me to create a static final - is that the reason I have a "the constructor Player(String) is undefined" error now?

    ...we have not covered constructors yet but have covered renaming of the object reference variable...could I not simply create the object as normal(Player tmpName = new Player;) first on each iteration and next line would simply rename the reference from an element of String from the array?


  • Registered Users, Registered Users 2 Posts: 19 Freqm0n


    cpuNames[] is an array of strings. You cannot initialise an element of it to be a Player object.
    So in any object creation I cant name the object reference variable from any stored variable???
    You are also trying to declare elements of the array as Players in the last line, which doesn't make sense.
    I may be missing the logic, the idea is to get a stored array of player names and then go about creating the player objects - how would you suggest I name these objects?
    You should use a post-increment for i also (i++), not a pre-increment (++i). Pre-increments increment the value before evaluating it. So in this case the firs iteration of the loop would be using the second element of the array instead of the first (index 1 not 0).
    Noted, thanks :)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 190 ✭✭NeutralHandle


    You need to create a class called Player if you haven't already done so.
    Simplest approach would be to provide a constructor that accepts the name as the parameter.
    You might want to use an array of Player objects instead of an array of strings.


Advertisement