Cannot display all items in foreach loop(Stack) in Java [closed]
I want to display all items in a stack but it doesn't work.
Here is my Classes:
public class CommandFactory
{
public Command getCommand(String type)
{
if(type.equalsIgnoreCase("c"))
{
return new CreateDVD();
}
//......Other commands
}
return null;
}
public interface DVD
{
int getDvdID();
String getTitle();
int getLength();
int getNumAvailable();
void setNumAvailable(int numAvailable);
String toString();
}
public class Movie implements DVD
{
private int dvdID;
private String title;
private int length;
private int numAvailable;
private String director;
public Movie(int dvdID, String title, int length, int numAvailable, String director)
{
//Constructor
}
public int getDvdID() { return dvdID; }
//...getters and setters
public String toString()
{
return String.format("DVD InformationnID: %dnTitle: %snLength: %d minsnNumber of available copies: %dnDirector: %s", dvdID, title, length, numAvailable, director);
}
}
public class CreateDVD implements Command
{
public Stack<DVD> dvdStack = new Stack<>();
private Stack<DVD> dvdUndoStack = new Stack<>();
private DVD dvd;
private DVDFactory dvdFactory = new DVDFactory();
private Scanner kb = new Scanner(System.in);
public void execute()
{
System.out.println("Please enter DVD type (mo=movie/mv=MV)");
String type = kb.next();
dvd = dvdFactory.getDVD(type);
dvdStack.push(dvd);
}
//other methods
}
The implementation below will only show the last item in the stack.
public class DVDHandler
{
Stack<Command> unDoCommandStack = new Stack<>();
Stack<Command> reDoCommandStack = new Stack<>();
Stack<DVD> dvdStack = new Stack<>();
CommandFactory commandFactory = new CommandFactory();
boolean exit = false;
public void run()
{
.
.
.
Scanner kb = new Scanner(System.in);
String input = kb.next();
Command command = commandFactory.getCommand(input);
if(command instanceof CreateDVD)
{
command.execute();
dvdStack = ((CreateDVD) command).getDvdStack();
unDoCommandStack.push(command);
}
.
.
.
else if(input.equalsIgnoreCase("s"))
{
System.out.println("Enter ID(enter a to show all): ");
String input1 = kb.next();
if(input1.equalsIgnoreCase("a"))
{
for (DVD dvd:dvdStack)
{
System.out.println(dvd.toString());
}
}
}
}
}
I have tried to print out the dvdStack and there are multiple items there. I don't why it doesn't work.
And then, I implemented it by using ArrayList below:
ArrayList<DVD> dvdArrayList = new ArrayList<>();
.
.
.
if(command instanceof CreateDVD)
{
command.execute();
dvdArrayList.add( (CreateDVD) command.getDvd() )
unDoCommandStack.push(command);
}
.
.
.
System.out.println("Enter ID(enter a to show all): ");
String input1 = kb.next();
if(input1.equalsIgnoreCase("a"))
{
for (DVD dvd:dvdArrayList)
{
System.out.println(dvd.toString());
}
}
This implementation works for displaying all items. However, I have been told to use stack.
Could someone help me to display all items in stack, please?
java foreach stack
closed as off-topic by JB Nizet, khelwood, Jim Garrison, GhostCat, Nicholas K Nov 25 '18 at 9:36
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – JB Nizet, khelwood, Jim Garrison, GhostCat, Nicholas K
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I want to display all items in a stack but it doesn't work.
Here is my Classes:
public class CommandFactory
{
public Command getCommand(String type)
{
if(type.equalsIgnoreCase("c"))
{
return new CreateDVD();
}
//......Other commands
}
return null;
}
public interface DVD
{
int getDvdID();
String getTitle();
int getLength();
int getNumAvailable();
void setNumAvailable(int numAvailable);
String toString();
}
public class Movie implements DVD
{
private int dvdID;
private String title;
private int length;
private int numAvailable;
private String director;
public Movie(int dvdID, String title, int length, int numAvailable, String director)
{
//Constructor
}
public int getDvdID() { return dvdID; }
//...getters and setters
public String toString()
{
return String.format("DVD InformationnID: %dnTitle: %snLength: %d minsnNumber of available copies: %dnDirector: %s", dvdID, title, length, numAvailable, director);
}
}
public class CreateDVD implements Command
{
public Stack<DVD> dvdStack = new Stack<>();
private Stack<DVD> dvdUndoStack = new Stack<>();
private DVD dvd;
private DVDFactory dvdFactory = new DVDFactory();
private Scanner kb = new Scanner(System.in);
public void execute()
{
System.out.println("Please enter DVD type (mo=movie/mv=MV)");
String type = kb.next();
dvd = dvdFactory.getDVD(type);
dvdStack.push(dvd);
}
//other methods
}
The implementation below will only show the last item in the stack.
public class DVDHandler
{
Stack<Command> unDoCommandStack = new Stack<>();
Stack<Command> reDoCommandStack = new Stack<>();
Stack<DVD> dvdStack = new Stack<>();
CommandFactory commandFactory = new CommandFactory();
boolean exit = false;
public void run()
{
.
.
.
Scanner kb = new Scanner(System.in);
String input = kb.next();
Command command = commandFactory.getCommand(input);
if(command instanceof CreateDVD)
{
command.execute();
dvdStack = ((CreateDVD) command).getDvdStack();
unDoCommandStack.push(command);
}
.
.
.
else if(input.equalsIgnoreCase("s"))
{
System.out.println("Enter ID(enter a to show all): ");
String input1 = kb.next();
if(input1.equalsIgnoreCase("a"))
{
for (DVD dvd:dvdStack)
{
System.out.println(dvd.toString());
}
}
}
}
}
I have tried to print out the dvdStack and there are multiple items there. I don't why it doesn't work.
And then, I implemented it by using ArrayList below:
ArrayList<DVD> dvdArrayList = new ArrayList<>();
.
.
.
if(command instanceof CreateDVD)
{
command.execute();
dvdArrayList.add( (CreateDVD) command.getDvd() )
unDoCommandStack.push(command);
}
.
.
.
System.out.println("Enter ID(enter a to show all): ");
String input1 = kb.next();
if(input1.equalsIgnoreCase("a"))
{
for (DVD dvd:dvdArrayList)
{
System.out.println(dvd.toString());
}
}
This implementation works for displaying all items. However, I have been told to use stack.
Could someone help me to display all items in stack, please?
java foreach stack
closed as off-topic by JB Nizet, khelwood, Jim Garrison, GhostCat, Nicholas K Nov 25 '18 at 9:36
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – JB Nizet, khelwood, Jim Garrison, GhostCat, Nicholas K
If this question can be reworded to fit the rules in the help center, please edit the question.
2
Post a complete minimal example reproducing the issue. We don't know which classes you're using, how you're using them, etc. Complete means that we should be able to copy the code, paste it in our IDE, and run it. Minimal means that it should contain the minimum code necessary to reproduce the issue.
– JB Nizet
Nov 25 '18 at 8:21
add a comment |
I want to display all items in a stack but it doesn't work.
Here is my Classes:
public class CommandFactory
{
public Command getCommand(String type)
{
if(type.equalsIgnoreCase("c"))
{
return new CreateDVD();
}
//......Other commands
}
return null;
}
public interface DVD
{
int getDvdID();
String getTitle();
int getLength();
int getNumAvailable();
void setNumAvailable(int numAvailable);
String toString();
}
public class Movie implements DVD
{
private int dvdID;
private String title;
private int length;
private int numAvailable;
private String director;
public Movie(int dvdID, String title, int length, int numAvailable, String director)
{
//Constructor
}
public int getDvdID() { return dvdID; }
//...getters and setters
public String toString()
{
return String.format("DVD InformationnID: %dnTitle: %snLength: %d minsnNumber of available copies: %dnDirector: %s", dvdID, title, length, numAvailable, director);
}
}
public class CreateDVD implements Command
{
public Stack<DVD> dvdStack = new Stack<>();
private Stack<DVD> dvdUndoStack = new Stack<>();
private DVD dvd;
private DVDFactory dvdFactory = new DVDFactory();
private Scanner kb = new Scanner(System.in);
public void execute()
{
System.out.println("Please enter DVD type (mo=movie/mv=MV)");
String type = kb.next();
dvd = dvdFactory.getDVD(type);
dvdStack.push(dvd);
}
//other methods
}
The implementation below will only show the last item in the stack.
public class DVDHandler
{
Stack<Command> unDoCommandStack = new Stack<>();
Stack<Command> reDoCommandStack = new Stack<>();
Stack<DVD> dvdStack = new Stack<>();
CommandFactory commandFactory = new CommandFactory();
boolean exit = false;
public void run()
{
.
.
.
Scanner kb = new Scanner(System.in);
String input = kb.next();
Command command = commandFactory.getCommand(input);
if(command instanceof CreateDVD)
{
command.execute();
dvdStack = ((CreateDVD) command).getDvdStack();
unDoCommandStack.push(command);
}
.
.
.
else if(input.equalsIgnoreCase("s"))
{
System.out.println("Enter ID(enter a to show all): ");
String input1 = kb.next();
if(input1.equalsIgnoreCase("a"))
{
for (DVD dvd:dvdStack)
{
System.out.println(dvd.toString());
}
}
}
}
}
I have tried to print out the dvdStack and there are multiple items there. I don't why it doesn't work.
And then, I implemented it by using ArrayList below:
ArrayList<DVD> dvdArrayList = new ArrayList<>();
.
.
.
if(command instanceof CreateDVD)
{
command.execute();
dvdArrayList.add( (CreateDVD) command.getDvd() )
unDoCommandStack.push(command);
}
.
.
.
System.out.println("Enter ID(enter a to show all): ");
String input1 = kb.next();
if(input1.equalsIgnoreCase("a"))
{
for (DVD dvd:dvdArrayList)
{
System.out.println(dvd.toString());
}
}
This implementation works for displaying all items. However, I have been told to use stack.
Could someone help me to display all items in stack, please?
java foreach stack
I want to display all items in a stack but it doesn't work.
Here is my Classes:
public class CommandFactory
{
public Command getCommand(String type)
{
if(type.equalsIgnoreCase("c"))
{
return new CreateDVD();
}
//......Other commands
}
return null;
}
public interface DVD
{
int getDvdID();
String getTitle();
int getLength();
int getNumAvailable();
void setNumAvailable(int numAvailable);
String toString();
}
public class Movie implements DVD
{
private int dvdID;
private String title;
private int length;
private int numAvailable;
private String director;
public Movie(int dvdID, String title, int length, int numAvailable, String director)
{
//Constructor
}
public int getDvdID() { return dvdID; }
//...getters and setters
public String toString()
{
return String.format("DVD InformationnID: %dnTitle: %snLength: %d minsnNumber of available copies: %dnDirector: %s", dvdID, title, length, numAvailable, director);
}
}
public class CreateDVD implements Command
{
public Stack<DVD> dvdStack = new Stack<>();
private Stack<DVD> dvdUndoStack = new Stack<>();
private DVD dvd;
private DVDFactory dvdFactory = new DVDFactory();
private Scanner kb = new Scanner(System.in);
public void execute()
{
System.out.println("Please enter DVD type (mo=movie/mv=MV)");
String type = kb.next();
dvd = dvdFactory.getDVD(type);
dvdStack.push(dvd);
}
//other methods
}
The implementation below will only show the last item in the stack.
public class DVDHandler
{
Stack<Command> unDoCommandStack = new Stack<>();
Stack<Command> reDoCommandStack = new Stack<>();
Stack<DVD> dvdStack = new Stack<>();
CommandFactory commandFactory = new CommandFactory();
boolean exit = false;
public void run()
{
.
.
.
Scanner kb = new Scanner(System.in);
String input = kb.next();
Command command = commandFactory.getCommand(input);
if(command instanceof CreateDVD)
{
command.execute();
dvdStack = ((CreateDVD) command).getDvdStack();
unDoCommandStack.push(command);
}
.
.
.
else if(input.equalsIgnoreCase("s"))
{
System.out.println("Enter ID(enter a to show all): ");
String input1 = kb.next();
if(input1.equalsIgnoreCase("a"))
{
for (DVD dvd:dvdStack)
{
System.out.println(dvd.toString());
}
}
}
}
}
I have tried to print out the dvdStack and there are multiple items there. I don't why it doesn't work.
And then, I implemented it by using ArrayList below:
ArrayList<DVD> dvdArrayList = new ArrayList<>();
.
.
.
if(command instanceof CreateDVD)
{
command.execute();
dvdArrayList.add( (CreateDVD) command.getDvd() )
unDoCommandStack.push(command);
}
.
.
.
System.out.println("Enter ID(enter a to show all): ");
String input1 = kb.next();
if(input1.equalsIgnoreCase("a"))
{
for (DVD dvd:dvdArrayList)
{
System.out.println(dvd.toString());
}
}
This implementation works for displaying all items. However, I have been told to use stack.
Could someone help me to display all items in stack, please?
java foreach stack
java foreach stack
edited Nov 25 '18 at 8:59
Jason Liu
asked Nov 25 '18 at 8:16
Jason LiuJason Liu
32
32
closed as off-topic by JB Nizet, khelwood, Jim Garrison, GhostCat, Nicholas K Nov 25 '18 at 9:36
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – JB Nizet, khelwood, Jim Garrison, GhostCat, Nicholas K
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by JB Nizet, khelwood, Jim Garrison, GhostCat, Nicholas K Nov 25 '18 at 9:36
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – JB Nizet, khelwood, Jim Garrison, GhostCat, Nicholas K
If this question can be reworded to fit the rules in the help center, please edit the question.
2
Post a complete minimal example reproducing the issue. We don't know which classes you're using, how you're using them, etc. Complete means that we should be able to copy the code, paste it in our IDE, and run it. Minimal means that it should contain the minimum code necessary to reproduce the issue.
– JB Nizet
Nov 25 '18 at 8:21
add a comment |
2
Post a complete minimal example reproducing the issue. We don't know which classes you're using, how you're using them, etc. Complete means that we should be able to copy the code, paste it in our IDE, and run it. Minimal means that it should contain the minimum code necessary to reproduce the issue.
– JB Nizet
Nov 25 '18 at 8:21
2
2
Post a complete minimal example reproducing the issue. We don't know which classes you're using, how you're using them, etc. Complete means that we should be able to copy the code, paste it in our IDE, and run it. Minimal means that it should contain the minimum code necessary to reproduce the issue.
– JB Nizet
Nov 25 '18 at 8:21
Post a complete minimal example reproducing the issue. We don't know which classes you're using, how you're using them, etc. Complete means that we should be able to copy the code, paste it in our IDE, and run it. Minimal means that it should contain the minimum code necessary to reproduce the issue.
– JB Nizet
Nov 25 '18 at 8:21
add a comment |
2 Answers
2
active
oldest
votes
Every time you enter "c", a new CreateDVD command is created, which has an empty stack.
Then it's executed, which adds a single DVD to its stack.
Then the DVDHandler's stack is replaced by the stack of the CreateDVD command:
dvdStack = ((CreateDVD) command).getDvdStack();
So it's not surprising to only find one DVD in the stack.
Thank you, I have improved the logic flow in class DVDCorner according to your answer. Now it works!
– Jason Liu
Nov 25 '18 at 13:10
add a comment |
Next time, please post more code so we can help with the issue more clearly. However, based on what you have here, a likely solution is using a for loop and the stack.elementAt method. For example:
public static void stackAttack (int inter){
Stack<Integer> stack=new Stack<Integer>();
stack.push(99);
stack.push(88);
stack.push(77);
stack.push(inter);
for(int n=stack.size(); n>0; n--) {
System.out.println(stack.elementAt(n-1));
}
}
public static void main (String args) {
stackAttack(69);
}
will print out each element of the stack in LIFO order without shrinking it. Hope this helps, and please include more info next time.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Every time you enter "c", a new CreateDVD command is created, which has an empty stack.
Then it's executed, which adds a single DVD to its stack.
Then the DVDHandler's stack is replaced by the stack of the CreateDVD command:
dvdStack = ((CreateDVD) command).getDvdStack();
So it's not surprising to only find one DVD in the stack.
Thank you, I have improved the logic flow in class DVDCorner according to your answer. Now it works!
– Jason Liu
Nov 25 '18 at 13:10
add a comment |
Every time you enter "c", a new CreateDVD command is created, which has an empty stack.
Then it's executed, which adds a single DVD to its stack.
Then the DVDHandler's stack is replaced by the stack of the CreateDVD command:
dvdStack = ((CreateDVD) command).getDvdStack();
So it's not surprising to only find one DVD in the stack.
Thank you, I have improved the logic flow in class DVDCorner according to your answer. Now it works!
– Jason Liu
Nov 25 '18 at 13:10
add a comment |
Every time you enter "c", a new CreateDVD command is created, which has an empty stack.
Then it's executed, which adds a single DVD to its stack.
Then the DVDHandler's stack is replaced by the stack of the CreateDVD command:
dvdStack = ((CreateDVD) command).getDvdStack();
So it's not surprising to only find one DVD in the stack.
Every time you enter "c", a new CreateDVD command is created, which has an empty stack.
Then it's executed, which adds a single DVD to its stack.
Then the DVDHandler's stack is replaced by the stack of the CreateDVD command:
dvdStack = ((CreateDVD) command).getDvdStack();
So it's not surprising to only find one DVD in the stack.
answered Nov 25 '18 at 9:16
JB NizetJB Nizet
543k588861011
543k588861011
Thank you, I have improved the logic flow in class DVDCorner according to your answer. Now it works!
– Jason Liu
Nov 25 '18 at 13:10
add a comment |
Thank you, I have improved the logic flow in class DVDCorner according to your answer. Now it works!
– Jason Liu
Nov 25 '18 at 13:10
Thank you, I have improved the logic flow in class DVDCorner according to your answer. Now it works!
– Jason Liu
Nov 25 '18 at 13:10
Thank you, I have improved the logic flow in class DVDCorner according to your answer. Now it works!
– Jason Liu
Nov 25 '18 at 13:10
add a comment |
Next time, please post more code so we can help with the issue more clearly. However, based on what you have here, a likely solution is using a for loop and the stack.elementAt method. For example:
public static void stackAttack (int inter){
Stack<Integer> stack=new Stack<Integer>();
stack.push(99);
stack.push(88);
stack.push(77);
stack.push(inter);
for(int n=stack.size(); n>0; n--) {
System.out.println(stack.elementAt(n-1));
}
}
public static void main (String args) {
stackAttack(69);
}
will print out each element of the stack in LIFO order without shrinking it. Hope this helps, and please include more info next time.
add a comment |
Next time, please post more code so we can help with the issue more clearly. However, based on what you have here, a likely solution is using a for loop and the stack.elementAt method. For example:
public static void stackAttack (int inter){
Stack<Integer> stack=new Stack<Integer>();
stack.push(99);
stack.push(88);
stack.push(77);
stack.push(inter);
for(int n=stack.size(); n>0; n--) {
System.out.println(stack.elementAt(n-1));
}
}
public static void main (String args) {
stackAttack(69);
}
will print out each element of the stack in LIFO order without shrinking it. Hope this helps, and please include more info next time.
add a comment |
Next time, please post more code so we can help with the issue more clearly. However, based on what you have here, a likely solution is using a for loop and the stack.elementAt method. For example:
public static void stackAttack (int inter){
Stack<Integer> stack=new Stack<Integer>();
stack.push(99);
stack.push(88);
stack.push(77);
stack.push(inter);
for(int n=stack.size(); n>0; n--) {
System.out.println(stack.elementAt(n-1));
}
}
public static void main (String args) {
stackAttack(69);
}
will print out each element of the stack in LIFO order without shrinking it. Hope this helps, and please include more info next time.
Next time, please post more code so we can help with the issue more clearly. However, based on what you have here, a likely solution is using a for loop and the stack.elementAt method. For example:
public static void stackAttack (int inter){
Stack<Integer> stack=new Stack<Integer>();
stack.push(99);
stack.push(88);
stack.push(77);
stack.push(inter);
for(int n=stack.size(); n>0; n--) {
System.out.println(stack.elementAt(n-1));
}
}
public static void main (String args) {
stackAttack(69);
}
will print out each element of the stack in LIFO order without shrinking it. Hope this helps, and please include more info next time.
answered Nov 25 '18 at 8:35
Daniel FelsenthalDaniel Felsenthal
713
713
add a comment |
add a comment |
2
Post a complete minimal example reproducing the issue. We don't know which classes you're using, how you're using them, etc. Complete means that we should be able to copy the code, paste it in our IDE, and run it. Minimal means that it should contain the minimum code necessary to reproduce the issue.
– JB Nizet
Nov 25 '18 at 8:21