Java program here I am stump with trying to fix this program we given. Basically
ID: 3834564 • Letter: J
Question
Java program here I am stump with trying to fix this program we given. Basically I need to convert it from using a linkedlist and have it run by using a stack.
Thanks for the help
Program
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Random;
class Player{
String playerCards[]=new String[5];
}
public class Cards {
public static void main(String[] args) throws FileNotFoundException,IOException{
int choice;
do{
BufferedReader in=new BufferedReader(new FileReader("<Enter the file path>"));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
LinkedList<String> card=new LinkedList<>();
String str;
while((str=in.readLine())!=null){//reading card from file
card.add(str);
}
Random ran=new Random();
LinkedList<String> suffledCard=new LinkedList<>();
while(card.size()!=0){//suffling the card
suffledCard.add(card.remove(ran.nextInt(card.size())));
}
System.out.println("Enter the number of player");
int n=Integer.parseInt(br.readLine());
Cards c=new Cards();
LinkedList<Player> players=new LinkedList<>();
for(int i=1;i<=n;i++){ //dealing the cards to player
Player p=new Player();
for(int j=0;j<5;j++){
p.playerCards[j]=suffledCard.removeFirst();
}
players.add(p);
}
//displaying cards of player
int i=1;
for(Player p:players){
System.out.println("Player "+(i++));
for(int j=0;j<5;j++){
System.out.print(p.playerCards[j]+" ");
}
System.out.println(" ");
}
System.out.println("Do you wish to continue?? Enter 1 for YES and 2 for NO");
choice=Integer.parseInt(br.readLine());
}while(choice==1);
}
}
Explanation / Answer
Hi,
Please see below the updated class. Please comment for any queries/feedbacks.
Thanks
Cards.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Random;
import java.util.Stack;
class Player {
String playerCards[]=new String[5];
}
public class Cards {
public static void main(String[] args) throws FileNotFoundException,IOException{
int choice;
do{
BufferedReader in=new BufferedReader(new FileReader("cards.txt"));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Stack<String> card=new Stack(); //Declaring stack for cards
String str;
while((str=in.readLine())!=null){//reading card from file
//push() Pushes an item onto the top of this stack.
card.push(str);
}
Random ran=new Random();
Stack<String> suffledCard=new Stack<>(); //Declaring stack for shuffled cards
while(card.size()!=0){//suffling the card
//push() Pushes an item onto the top of this stack.
suffledCard.push(card.remove(ran.nextInt(card.size())));
}
System.out.println("Enter he no of Players: ");
int n=Integer.parseInt(br.readLine());
Cards c=new Cards();
Stack<Player> players=new Stack<>();//Declaring stack for players
for(int i=1;i<=n;i++){ //dealing the cards to player
Player p=new Player();
for(int j=0;j<5;j++){
//pop() Removes the object at the top of this stack and returns that object as the value of this function
p.playerCards[j]=suffledCard.pop();
}
players.add(p);
}
//displaying cards of player
int i=1;
for(Player p:players){
System.out.println("Player "+(i++));
for(int j=0;j<5;j++){
System.out.print(p.playerCards[j]+" ");
}
System.out.println(" ");
}
System.out.println("Do you wish to continue?? Enter 1 for YES and 2 for NO");
choice=Integer.parseInt(br.readLine());
}while(choice==1);
}
}
cards.txt'
S4
HK
CQ
D2
S6
DJ
H9
C3
H5
DK
S8
CJ
C10
D7
HQ
S6
H7
D4
S8
C2
Sample output:
Enter he no of Players:
4
Player 1
H9 D7 C3 D4 S8
Player 2
DJ CQ CJ HQ H5
Player 3
C2 DK H7 S4 S6
Player 4
S6 S8 C10 HK D2
Do you wish to continue??
Enter 1 for YES and 2 for NO
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.