Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Angry Trying to initialize arrays

    Alright, noob question. I've search on multiple sites but can't figure what's wrong. I've tried writing things a billion different ways. I'm making a card game and have a class I used to assign all my card variables.
    However, it's giving me multiple errors and I can't seem to be able to access the variables from my other classes.

    Java Code:
    package com.summoners.Screen;
    
    class Cards {
        public static String[] names;
    
            name = new String[1000];
            atk = new int[1000];
               
            // initialize card data
            name[0] = "John";
            atk[0] = 30;
            
            name[1] = "Bob";
            atk[1] = 2;
            
            name[2] = "Mark";
            atk[2] = 5;
        
        }
    }

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,076
    Rep Power
    6

    Default Re: Trying to initialize arrays

    No method anywhere. That's all I'm going to say.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,762
    Rep Power
    5

    Default Re: Trying to initialize arrays

    This shouldn't even compile. names vs name.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Trying to initialize arrays

    ... and those parallel arrays are not done; even a single array is so Fortranesque ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Trying to initialize arrays

    I (think I) fixed it. Thanks guys.

    Java Code:
    package com.summoners.Screen;
    
    class Cards {
        public static String[] name;
        public static int[] atk;
    
        public static void initCardData(){
            name = new String[1000];
            atk = new int[1000];
               
            // initialize card data
            name[0] = "John";
            atk[0] = 30;
            
            name[1] = "Bob";
            atk[1] = 2;
            
            name[2] = "Mark";
            atk[2] = 5;
        }   
    }

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,076
    Rep Power
    6

    Default Re: Trying to initialize arrays

    Well at least it will compile, but it isn't fixed until you know how to remove all those static keywords and you use only one array and a class to store the two pieces of data per index.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Trying to initialize arrays

    Quote Originally Posted by gimbal2 View Post
    Well at least it will compile, but it isn't fixed until you know how to remove all those static keywords and you use only one array and a class to store the two pieces of data per index.
    I don't know how to use getters & setters with arrays. :(

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,076
    Rep Power
    6

    Default Re: Trying to initialize arrays

    Apparently that does not trigger you to go learn. That is a situation I hope that will resolve itself sooner rather than later, at this point you should be driven by a quest for knowledge and understanding and not to "make stuff work".
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Trying to initialize arrays

    Quote Originally Posted by supremegrandruler View Post
    I don't know how to use getters & setters with arrays. :(
    Neither do I because arrays don't have methods; putting the same old gibberish (read: non-OO stuff) in a main( ... ) method doesn't instantly make your code 'elegant' code. Define a small class and stick objects of that class in a List and forget about those parallel arrays and forget about arrays all together; it'll make your code so much more flexible.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Trying to initialize arrays

    Just to explain in more detail how you could build up your application in an object oriented way. (Please be aware that this is a little guessing because you didn't give any details so far):

    Your game has Player which have a Name and a "atk" value. (And maybe even more. Maybe your cardgame can run multiple games so it has a reference to a game instance the player is playing / part of.).
    So you can create a class Player with these fields inside.
    Your game holds a List of Players (Or maybe again your 100 but that only makes sense when you want to have exactly 100 Player). So you can get a List<Player> in which you store your player objects.

    You create objects like that. Maybe you have some kind of Gameboard on which cards can be placed. In such a case you can have a Gameboard class or something like that.

    Whenever you need to implement something, then you put it, where it really belongs to. So for example: A player can draw a card. This could be the function "DrawCard()".

    Did this help a little bit? Or do you need more help regarding such an object oriented design? In such a case you might want to give us details what you have in mind and we might give you suggestions how it could be done.

    With kind regards,

    Konrad

  11. #11
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Trying to initialize arrays

    Quote Originally Posted by kneitzel View Post
    Just to explain in more detail how you could build up your application in an object oriented way. (Please be aware that this is a little guessing because you didn't give any details so far):

    Your game has Player which have a Name and a "atk" value. (And maybe even more. Maybe your cardgame can run multiple games so it has a reference to a game instance the player is playing / part of.).
    So you can create a class Player with these fields inside.
    Your game holds a List of Players (Or maybe again your 100 but that only makes sense when you want to have exactly 100 Player). So you can get a List<Player> in which you store your player objects.

    You create objects like that. Maybe you have some kind of Gameboard on which cards can be placed. In such a case you can have a Gameboard class or something like that.

    Whenever you need to implement something, then you put it, where it really belongs to. So for example: A player can draw a card. This could be the function "DrawCard()".

    Did this help a little bit? Or do you need more help regarding such an object oriented design? In such a case you might want to give us details what you have in mind and we might give you suggestions how it could be done.

    With kind regards,

    Konrad
    Thanks, very useful. I'm used to modding for WarCraft III so that's how we've always done it. I'm beginning to think a lot more in OO ways and studying from a quite a few different sources, including the Java website.

    Thanks, you guys are very helpful. :)

Similar Threads

  1. Replies: 11
    Last Post: 04-11-2012, 05:57 PM
  2. ObjectInputStream does not initialize
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 06-03-2009, 08:11 AM
  3. Int does not initialize, will this work?
    By starchildren3317 in forum New To Java
    Replies: 2
    Last Post: 07-09-2008, 10:42 PM
  4. Initialize variables before use
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 11:22 AM
  5. I do not know how to initialize the two variables
    By Daniel in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 04:42 AM

Posting Permissions

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