hi.. i'm still new to java and i have a project to work on regarding card games. I want to shuffle the deck but i just can't get it.Shuffling the deck must be alternate each element. I don't know if i'm doing the right thing.
I keep getting a " Exception in thread "main" java.lang.NullPointerException". Thanks!
Here's my code:
import java.io.*;
import java.util.*;
public class Deck{
Vector deck; // create a 52-card deck
final String[] suit = {"D", "H", "S", "C"};
final String[] rank = {"A","K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"};
public Deck(){
// creating a list
List<String> list = new ArrayList<String>();
for (int i = 0; i < suit.length; i++)
{
for (int j = 0; j < rank.length; j++)
list.add(rank[j] + suit[i]);
}
// convert the array to vector
Vector deck = new Vector(list);
// access elements
System.out.println("Initial Deck:");
Enumeration deck1 = deck.elements();
while (deck1.hasMoreElements())
{
System.out.println(deck1.nextElement().toString());
}
}
public void shuffle()throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
System.out.println ("Enter the desired number for shuffle: ");
String temp = br.readLine();
int count = Integer.parseInt(temp);
for (int b=0; b<=count; b++)
{
for (int i=1;i<(deck.size())/2;i+=2)
{
for (int j=26;j<(deck.size())/2;j+=2)
{
Collections.swap(deck, i, j);}
}
} System.out.println("Shuffled Deck:\n" + deck);
}
public static void main (String[] args) throws IOException{
Deck cardeck = new Deck();
cardeck.shuffle();
}
}