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(); } }