Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Having some trouble with my arrayList

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Having some trouble with my arrayList

    Hey everyone. If anybody can help me with this problem I'm having then I will be eternally grateful. I'm trying to give a String variable card1 a value of an arrayList I have at either indexes 0 or 1. However, my compiller keeps telling me that the way I'm saying it is an invalid start of expression, and I'm wracking my brain trying to fix it. Can anyone please help.

    Heres my code. The particular problem is in the Game class:

    import java.util.*;
    public class Card
    {
    	private int value = 0;
    	private int suit = 0;
     
     
    	public Card(int value, int suit) 
    	{
    		this.value = value;
    		this.suit = suit;
    	}
     
    	public String toString() 
    	{
    		return getValue() + " of " + getSuit();
    	}
     
    	public String getValue()
    	{ 
    		if (value == 1)
    		{
    			return "Ace";
    		}
    		else if (value == 11)
    		{
    			return "Jack";
    		}
    		else if (value == 12)
    		{
    			return "Queen";
    		}
    		else if (value == 13)
    		{
    			return "King";
    		}
    		else
    			return new Integer(value).toString();
    	}
     
    	public String getSuit()
    	{
    		if(suit == 1)
    		{
    			return "Spades";
    		}
    		else if(suit == 2)
    		{
    			return "Clubs";
    		}
    		else if(suit == 3)
    		{
    			return "Hearts";
    		}
    		else
    		{
    			return "Diamonds";
    		}
    	}
     
    	public void setSuit(int suit)
    	{
    		this.suit = suit;
    	}
     
     
    }


    import java.util.*;
    public class Game
    {
    	private ArrayList<Card> cardDeck;
    	private int cards = 52;
    	private Player player1;
    	private Player player2;
    	private String playerOne = null;
    	private String playerTwo = null;
    	private String card1, card2 = null;
     
    	public Game() 
    	{
     
    	}
     
    	public void deckGenerate()
    	{
    		int i, j = 0;
     
    		for (i = 1; i <= 4; i++)
    		{
    			for(j = 1; j <= 13; j++)
    			{
    				Card card = new Card(i ,j);
    				cardDeck.add(card);
    			}
    		}
    	}
     
    	public void deckShuffle()
    	{
    		int i = 0;
    		Random randomCard = new Random();
     
    		for(i = 0; i < cards-1; i++)
    		{
    			int newIndex = randomCard.nextInt(cards-i)+i;
    			Card tempCard = cardDeck.get(i);
    			cardDeck.set(i, cardDeck.get(newIndex));
    			cardDeck.set(newIndex, tempCard);
    		}
    	}
     
    	public void gameStart()
    	{
    		Scanner keyboard = new Scanner(System.in);
    		cardDeck = new ArrayList<Card>(cards);
     
    		deckGenerate();
    		deckShuffle();
     
    		System.out.println("Enter player one's name.");
    		playerOne = keyboard.nextLine();
    		//System.out.println("Enter player two's name.");
    		//playerTwo = keyboard.nextLine();
     
    		player1 = new Player(playerOne);
    		//player2 = new Player(playerTwo);
     
    		System.out.println("Please choose your first number, between 1 and 52.");
    		int choice1 = keyboard.nextInt();
     
    		if (choice1 == 1){
    			card1 = cardDeck<0>.getValue();
    			}
    		else{
    			card1 = cardDeck<1>.getValue();}
     
    		System.out.println("Please choose your second number, between 1 and 52.");
    		int choice2 = keyboard.nextInt();
     
    		if (choice2 == 1){
    			card2 = cardDeck<0>.getValue();}
    		else{ 
    			card2 = cardDeck<1>.getValue();}
     
     
    	}
     
     
    }


    import java.util.*;
    public class Player
    {
    	private String playerName = null;
    	private ArrayList<Card> playerHand;
     
    	public Player(String playerName)
    	{
    		this.playerName = playerName;
    		playerHand = new ArrayList<Card>();
    	}
     
    	public String getName()
    	{
    		return playerName;
    	}
     
     
     
     
    }


    import java.util.*;
    public class PrimaryGame
    {
    	public static void main(String[] args)
    	{
    		Game newGame = new Game();
     
    		newGame.gameStart();	
     
     
     
     
    	}
     
     
     
    }
    Last edited by bankston13; April 26th, 2012 at 11:19 PM.


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Having some trouble with my arrayList

    Normally I wouldn't care if you didn't use the code or highlight tags but this is just hard to look at. Go here to see how to use code tags, and to get java-like syntax highlighting you would just use [code=java] instead of [code]. Also, please post the full error message(s).

  3. #3
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Having some trouble with my arrayList

    Thank you very much for the edit, much better on the eyes. The problem lies in cardDeck<0> as this is not how you would reference the object in the ArrayList at index zero. Rather, you would use the get method i.e. cardDeck.get(0).


    EDIT:
    Seems we were writing our posts at the same time. Anyway, what is written above should help you out and again thank you for bearing with my nit-picking.
    Last edited by KucerakJM; April 26th, 2012 at 11:32 PM. Reason: Double post then post while starter is posting, gotta love it.

  4. The Following User Says Thank You to KucerakJM For This Useful Post:

    bankston13 (April 30th, 2012)

  5. #4
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Having some trouble with my arrayList

    Sorry about that. I can definately understand the frustration there. I've updated my original post with the code style. This is one line of error messages, but it applies to all 4 of the same instances.


    Game.java:65: illegal start of expression
    card1 = cardDeck<0>.getValue();

    Its specifically pointing to the period before getValue().

  6. #5
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Having some trouble with my arrayList

    That was definately the problem. Thank you so much for your help. I really appreciate it.

  7. #6
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Having some trouble with my arrayList

    Fantastic, hope the rest of the coding goes well.

Similar Threads

  1. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  2. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM