Results 1 to 11 of 11
Thread: Trying to initialize arrays
- 04-01-2014, 01:58 PM #1
Member
- Join Date
- Mar 2014
- Posts
- 72
- Rep Power
- 0
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; } }
- 04-01-2014, 02:02 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 04-01-2014, 02:02 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Trying to initialize arrays
This shouldn't even compile. names vs name.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 04-01-2014, 03:12 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Trying to initialize arrays
... and those parallel arrays are not done; even a single array is so Fortranesque ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 04-01-2014, 04:13 PM #5
Member
- Join Date
- Mar 2014
- Posts
- 72
- Rep Power
- 0
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; } }
- 04-01-2014, 04:35 PM #6
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 04-01-2014, 05:05 PM #7
Member
- Join Date
- Mar 2014
- Posts
- 72
- Rep Power
- 0
- 04-01-2014, 05:12 PM #8
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 04-01-2014, 07:03 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Trying to initialize 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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 04-01-2014, 08:50 PM #10
Senior Member
- Join Date
- Feb 2014
- Posts
- 447
- Rep Power
- 8
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
- 04-05-2014, 01:30 PM #11
Member
- Join Date
- Mar 2014
- Posts
- 72
- Rep Power
- 0
Re: Trying to initialize arrays
Similar Threads
-
To initialize or not to initialize - when initialized value is not used
By Vizoere in forum New To JavaReplies: 11Last Post: 04-11-2012, 06:57 PM -
ObjectInputStream does not initialize
By Singing Boyo in forum New To JavaReplies: 1Last Post: 06-03-2009, 09:11 AM -
Int does not initialize, will this work?
By starchildren3317 in forum New To JavaReplies: 2Last Post: 07-09-2008, 11:42 PM -
Initialize variables before use
By Java Tip in forum Java TipReplies: 0Last Post: 12-22-2007, 12:22 PM -
I do not know how to initialize the two variables
By Daniel in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 05:42 AM
Bookmarks