Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: output ArrayList Object String

  1. #1
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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
    Java 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);
     		
     	}
    	
    	
    		
    		
    	}
    Cards.java
    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
    Java 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());
    	
    	   }
    	
    	
    
    	
    	
    	
    	   }
    Thanks in advance
    Russ

    edit: i am getting null values every time but in debug the objects are being added to the arrayList
    Last edited by Russd772; 12-29-2012 at 06:57 PM. Reason: added info

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: output ArrayList Object String

    1. What line in your code above either throws a NullPointerException or shows null values?
    2. 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.
    3. Why are you even creating a MakeDeck instance when all you're calling are static methods?
    4. 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 09:08 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

    Java Code:
     public Cards(String name, int value, int value2){
          
     }
    Where do you set any of the class fields in this constructor?

  4. #4
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: output ArrayList Object String

    Quote Originally Posted by Russd772 View Post
    thanks you, my mistake was obvious after you said that. I was using static because Eclipse told me to...
    No it didn't. Java told you that you couldn't access data in a non-static way. The solution is not to make things static but to not try to access instance methods in a static way -- to only call methods on instances. You fixed the wrong thing.
    DarrylBurke likes this.

Similar Threads

  1. Replies: 4
    Last Post: 04-07-2012, 12:55 PM
  2. Replies: 2
    Last Post: 11-16-2010, 06:51 PM
  3. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  4. Replies: 1
    Last Post: 03-04-2009, 07:14 PM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 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
  •