Results 1 to 5 of 5
Thread: output ArrayList Object String
- 12-29-2012, 05:08 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
output ArrayList Object String
Hey all,
I got the idea to store objects in an arraylist here and i think i have accomplished that, but what im having trouble with is getting the String name of the object stored in the arrayList.
im still new to java and i dont mind looking it up myself so if anyone wants to just post a link or something ill be happy to go do the reading myself.
heres some code.
BlackJack.java
this is the main class
Cards.javaJava Code:import java.util.Random; import java.util.Scanner; public class BlackJack { static int i; static int handsize; public static void main(String[] args) { MakeDeck test = new MakeDeck(); // set Handsize test.deckOfCards(4); } }
this is the constructor for the Card object im storing in the ArrayList
Java Code:public class Cards { String name; int value; int value2; public Cards(String name, int value, int value2){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public int getValue2() { return value2; } public void setValue2(int value2) { this.value2 = value2; } }
MakeDeck.java
This is the ArrayList and output of a random card from the deck, right now im only concerned with output of the string data and will figure out the Integers later
Thanks in advanceJava Code:import java.util.ArrayList; import java.util.Collections; public class MakeDeck { static Cards aceHearts = new Cards("Ace Of Hearts", 11, 1); static Cards twoHearts = new Cards("Two Of Hearts", 2, 0); static Cards threeHearts = new Cards("Three Of Hearts", 3, 0); static Cards fourHearts = new Cards("Four Of Hearts", 4, 0); static Cards fiveHearts = new Cards("Five Of Hearts", 5, 0); static Cards sixHearts = new Cards("Six Of Hearts", 6, 0); static Cards sevenHearts = new Cards("Seven Of Hearts", 7, 0); static Cards eightHearts = new Cards("Eight Of Hearts", 8, 0); static Cards nineHearts = new Cards("Nine Of Hearts", 9, 0); static Cards tenHearts = new Cards("Ten Of Hearts", 10, 0); static Cards jackHearts = new Cards("Jack Of Hearts", 10, 0); static Cards queenHearts = new Cards("Queen Of Hearts", 10, 0); static Cards kingHearts = new Cards("King Of Hearts", 10, 0); static Cards aceClubs = new Cards("Ace Of Clubs", 11, 1); static Cards twoClubs = new Cards("Two Of Clubs", 2, 0); static Cards threeClubs = new Cards("Three Of Clubs", 3, 0); static Cards fourClubs = new Cards("Four Of Clubs", 4, 0); static Cards fiveClubs = new Cards("Five Of Clubs", 5, 0); static Cards sixClubs = new Cards("Six Of Clubs", 6, 0); static Cards sevenClubs = new Cards("Seven Of Clubs", 7, 0); static Cards eightClubs = new Cards("Eight Of Clubs", 8, 0); static Cards nineClubs = new Cards("Nine Of Clubs", 9, 0); static Cards tenClubs = new Cards("Ten Of Clubs", 10, 0); static Cards jackClubs = new Cards("Jack Of Clubs", 10, 0); static Cards queenClubs = new Cards("Queen Of Clubs", 10, 0); static Cards kingClubs = new Cards("King Of Clubs", 10, 0); static Cards aceSpades = new Cards("Ace Of Spades", 11, 1); static Cards twoSpades = new Cards("Two Of Spades", 2, 0); static Cards threeSpades = new Cards("Three Of Spades", 3, 0); static Cards fourSpades = new Cards("Four Of Spades", 4, 0); static Cards fiveSpades = new Cards("Five Of Spades", 5, 0); static Cards sixSpades = new Cards("Six Of Spades", 6, 0); static Cards sevenSpades = new Cards("Seven Of Spades", 7, 0); static Cards eightSpades = new Cards("Eight Of Spades", 8, 0); static Cards nineSpades = new Cards("Nine Of Spades", 9, 0); static Cards tenSpades = new Cards("Ten Of Spades", 10, 0); static Cards jackSpades = new Cards("Jack Of Spades", 10, 0); static Cards queenSpades = new Cards("Queen Of Spades", 10, 0); static Cards kingSpades = new Cards("King Of Spades", 10, 0); static Cards aceDiamonds = new Cards("Ace Of Diamonds", 11, 1); static Cards twoDiamonds = new Cards("Two Of Diamonds", 2, 0); static Cards threeDiamonds = new Cards("Three Of Diamonds", 3, 0); static Cards fourDiamonds = new Cards("Four Of Diamonds", 4, 0); static Cards fiveDiamonds = new Cards("Five Of Diamonds", 5, 0); static Cards sixDiamonds = new Cards("Six Of Diamonds", 6, 0); static Cards sevenDiamonds = new Cards("Seven Of Diamonds", 7, 0); static Cards eightDiamonds = new Cards("Eight Of Diamonds", 8, 0); static Cards nineDiamonds = new Cards("Nine Of Diamonds", 9, 0); static Cards tenDiamonds = new Cards("Ten Of Diamonds", 10, 0); static Cards jackDiamonds = new Cards("Jack Of Diamonds", 10, 0); static Cards queenDiamonds = new Cards("Queen Of Diamonds", 10, 0); static Cards kingDiamonds = new Cards("King Of Diamonds", 10, 0); static ArrayList<Cards> deck; public static void deckOfCards(int index){ deck = new ArrayList<Cards>(); deck.add(aceHearts);deck.add(twoHearts);deck.add(threeHearts);deck.add(fourHearts); deck.add(fiveHearts);deck.add(sixHearts);deck.add(sevenHearts);deck.add(eightHearts); deck.add(nineHearts);deck.add(tenHearts);deck.add(jackHearts);deck.add(queenHearts); deck.add(kingHearts); deck.add(aceClubs);deck.add(twoClubs);deck.add(threeClubs);deck.add(fourClubs); deck.add(fiveClubs);deck.add(sixClubs);deck.add(sevenClubs);deck.add(eightClubs); deck.add(nineClubs);deck.add(tenClubs);deck.add(jackClubs);deck.add(queenClubs); deck.add(kingClubs); deck.add(aceSpades);deck.add(twoSpades);deck.add(threeSpades);deck.add(fourSpades); deck.add(fiveSpades);deck.add(sixSpades);deck.add(sevenSpades);deck.add(eightSpades); deck.add(nineSpades);deck.add(tenSpades);deck.add(jackSpades);deck.add(queenSpades); deck.add(kingSpades); deck.add(aceDiamonds);deck.add(twoDiamonds);deck.add(threeDiamonds);deck.add(fourDiamonds); deck.add(fiveDiamonds);deck.add(sixDiamonds);deck.add(sevenDiamonds);deck.add(eightDiamonds); deck.add(nineDiamonds);deck.add(tenDiamonds);deck.add(jackDiamonds);deck.add(queenDiamonds); deck.add(kingDiamonds); Collections.shuffle(deck); /* This can be ignored was just using for testing some stuff int x = deck.get(2).getValue(); int y = deck.get(8).getValue(); int z = x + y; */ System.out.println(deck.get(index).getName() + " "+ deck.get(index).getValue()); } }
Russ
edit: i am getting null values every time but in debug the objects are being added to the arrayListLast edited by Russd772; 12-29-2012 at 05:57 PM. Reason: added info
-
Re: output ArrayList Object String
- What line in your code above either throws a NullPointerException or shows null values?
- Why are those variables static? Remember that static is evil and should only be used in specific situations, and this isn't one of them.
- Why are you even creating a MakeDeck instance when all you're calling are static methods?
- Please fix your code formatting, in particular your indentation which is not consistent and your use of white space which should be used judiciously but not overused.
Last edited by Fubarable; 12-29-2012 at 08:08 PM.
-
Re: output ArrayList Object String
Ah heck, your look at your Cards constructor. It should be obvious why Cards only holds null values as you set no fields.
Where do you set any of the class fields in this constructor?Java Code:public Cards(String name, int value, int value2){ }
- 12-29-2012, 09:24 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
Re: output ArrayList Object String
thanks you, my mistake was obvious after you said that. I was using static because Eclipse told me to... the tutorials i was going through used "public static void methodName()" so i did the same and eclipse was telling me i couldnt use a non static object in a static method, so in short i was being a noob. but i fixed that as well, thnaks for pointing that out for me. and im not really sure whats wrong with the code formatting, i know my closing brackets are spaced out too much right now but the project is not finished so i wasnt really concerned about that. its mostly from adding and deleting things to see what happens when i do something a diffrent way and they just end up getting further and further apart.
anyways Thanks for the valuable info and i will keep this in mind next time
Russ
-
Re: output ArrayList Object String
Similar Threads
-
Converting ArrayList<Object> into a String[] of their toString()s
By Psygnosys in forum New To JavaReplies: 4Last Post: 04-07-2012, 11:55 AM -
A private static ArrayList hold an object and static getter ArrayList
By Louis in forum New To JavaReplies: 2Last Post: 11-16-2010, 05:51 PM -
add object to ArrayList (object is from extends other class)
By mBull in forum Java AppletsReplies: 3Last Post: 03-15-2010, 08:44 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks