Why do I get a null value?
package wargame;
import java.util.*;
public class Table {
public int n, p, c;
public LinkedList<String> shuffle = new LinkedList<String>();
public LinkedList<String> card = new LinkedList<String>();
public CompleteDeckCard getcard = new CompleteDeckCard();
LinkedList<String> playerNum = new LinkedList();
LinkedList<String> shuffledCards = cardShuffle();
public LinkedList<String> rank = new LinkedList<String>();
public CompleteDeckCard getrank = new CompleteDeckCard();
boolean winner;
//Determining the number of shuffles
public int tablePlay() {
System.out.println ("How many shuffles do you want? ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println ("Your input is: " + n);
System.out.println ("Shuffling cards, please wait a moment ...");
return n;
}
//Shuffling of cards
public LinkedList<String> cardShuffle(){
card = getcard.cards();
for (int sc = 0; sc < n; sc++){
for( int x=0, y=26; x<26; x++,y++){
shuffle.add(card.get(x));
shuffle.add(card.get(y));
}
card.clear();
card.addAll(shuffle);
shuffle.clear();
}
//System.out.println("New Deck: " + shuffle);
return card;
}
//Determining the number of players
public LinkedList<String>[] player() {
rank = getrank.ranks();
Scanner in = new Scanner(System.in);
p = in.nextInt();
System.out.println ("Number of players: " + p);
System.out.println ("Creating list of players ... " );
//return p;
//Distribution of cards
shuffledCards = cardShuffle();
LinkedList<String>[] playerNum = new LinkedList[p];
for (int z=0; z<p; z++){
playerNum[z] = new LinkedList<String>();
System.out.println ("playerNum[" + z + "]");
}
while (!shuffledCards.isEmpty()){
for (int v=0; v<p; v++){
playerNum[v].addFirst(shuffledCards.pollFirst());
if (playerNum[v].contains(null))
playerNum[v].remove(null);
}
}
System.out.println (" ");
System.out.println ("Distributing cards ...");
for (int m=0; m<p; m++){
System.out.println ("playerNum[" + m + "]" + playerNum[m] );
}
//Getting the top card
while (!winner){
LinkedList<String> getTopCards = new LinkedList<String>();
for (int g=0; g<p; g++){
getTopCards.add(playerNum[g].pollFirst());
}
System.out.println (" ");
System.out.println ("Top Cards: " + getTopCards);
System.out.println (" ");
//Comparing the values
LinkedList<Integer> evaluate = new LinkedList<Integer>();
Collections.reverse(rank);
System.out.println ("Order of card: " +rank);
for( int s = 0; s < getTopCards.size(); s++){
evaluate.add(rank.indexOf(getTopCards.get(s)));
}
System.out.println(" ");
//System.out.println("Ranking: " + evaluate);
int e = evaluate.indexOf(Collections.max(evaluate));
System.out.println( "playerNum[" + e + "] is the winner. ");
System.out.println(" " );
//Collect the initial cards and give to the winner of the round
LinkedList<String> entry = new LinkedList<String>();
for (int d=e; d<p; d++){
entry.add(getTopCards.get(d));
}
if (e!=0){
for (int f=0; f<e; f++){
entry.add(getTopCards.get(f));
}
}
playerNum[e].addAll(entry);
while (playerNum[e].contains(null)){
playerNum[e].remove(null);
}
System.out.println ("Collected cards: " + entry);
//Determine number of rounds
LinkedList<Integer> rounds = new LinkedList<Integer> ();
for (int g=0; g<p; g++){
System.out.println (playerNum[g]);
//for (int h=0; h<p; h++){
if (playerNum[g].size() == 52){
winner = true;
break;
}
}
//}
}
return playerNum;
}
}
Re: Why do I get a null value?
It's always good to read the rules of a forum before posting in it. Even without the rules, you should have to common decency and sense to not just dump a wad of code without offering any explanation whatsoever. So, if you want help, there are a few things I suggest you do.
First, be more specific. If you program is giving null values, it should also be throwing NullPointerExceptions that will terminate your program. Post those here in their entirety. Furthermore, it would be nice to have some context--what are you trying to make? Looking at the exceptions, where is the problem originating? Find the source of your problem, and ask a specific question.
Second, refine your code. Don't lazily dump it all here; look at your exceptions and problem solve until you've found the section of code causing the problem. Post that section only. The reason for this is simple--it makes forum members' job of helping you easier because there is less to sift through.
Finally, use the correct code tags when posting code. If you don't know how to do that, read my signature. Using code tags formats your code so that it can be more easily read.
Trust me, people on this forum want to and will help you, but you have to show that you appreciate their time by making their job as easy as you can.
Re: Why do I get a null value?
I am sorry about the long code. I thought it would be helpful for readers to see everything. I really don't know yet how to implement the Exceptions.
I seem to have traced the problem. I just do not know how to tell the program to skip the particular list and proceed to the next entry.
Code Java:
while (!winner){
LinkedList<String> getTopCards = new LinkedList<String>();
for (int g=0; g<p; g++){
getTopCards.add(playerNum[g].pollFirst());
I am trying to loop this particular until all the cards go to a single player. What happens is whenever one of the players loses all his cards, the program register null only to be able to proceed to the goal of giving all the cards to only 1 player.