Reputation:
I am practicing Try-Catch, Exception Handling, and Stacks and Queues, so although the code may be impractical, it is useful for my practice.
public class Practice {
public static void main(String args[]){
Stack<String> sStack = new Stack<String>();
Queue<String> sQueue = new LinkedList<String>();
Scanner input = new Scanner(System.in);
String inString = " ";
boolean done = false;
do{
try{
while(!inString.equals("")){
System.out.println("Enter a string: ");
inString = input.nextLine().toString();
addS(sStack,inString);
}
done = true;
}catch(Exception e){}
sStack.pop();
for(int i = 0; i<sStack.size()+1;i++){
remS(sStack);
}
}while(done == false);
}
The code is intended to loop through and populate an empty stack based on user input, and then loop through the stack and remove each element while giving a prompt. The first set I input is:
[art, bat, cat, dock, eye, father]
the second set is:
[art, bat, cat, dock, eye, father, kid]
I noticed that no matter how many elements I input it will only remove up to 4 elements. I would like your help in figuring out why. I tried playing around with the loop's bounds as you can see, but to no avail. No matter where I start or bound, it will always end up popping only 4 elements max.
The addS and remS functions I provide below
public static void addS(Stack t, String s){
t.push(s);
System.out.printf("%s was added to the stack %s", s, t);
System.out.println();
}
public static void remS(Stack t){
System.out.printf("\n%s has been popped.",t);
t.pop();
System.out.printf("The current elements are now: ");
System.out.print(t);
System.out.printf("The next element to be popped is %s", t.peek());
}
Upvotes: 0
Views: 10472
Reputation: 101
use int i=1
intead of i=0
. for example(int i=1;i<=n;i++)
.
import java.io.*;
import java.util.*;
public class Rev
{
public static void main(String args[]) throws IOException
{
Stack<String> v=new Stack<String>();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String[] arr=new String[n];
//String str=br.readLine();
for(int i=0;i<n;i++)
{
arr[i]=br.readLine();
}
System.out.println("entered data is");
for(int i=0;i<n;i++)
{
System.out.println(arr[i]);
}
for(int i=0;i<n;i++)
{
v.push(arr[i]);
}
System.out.println("getting the element");
System.out.println(v.get(1));
v.set(1,"15");
System.out.println(v.get(1));
System.out.println("reversed String is");
for(int i=1;i<=n;i++)
{
System.out.println(v.pop());
}
}
}
Upvotes: 0
Reputation: 660397
This is a frequently asked question. This is a good opportunity for you to first, learn how to use your debugger, and second, learn how to think about programming problems logically.
for(int i = 0; i<sStack.size()+1;i++){
remS(sStack);
}
This always removes half the items in the stack. Work out why by emulating the operation of the loop on paper and you will quickly see why.
Example of what I mean. Write down:
stack contains A, B, C, D
i is 0
test loop condition: 0 is smaller than 4 + 1, so we enter the loop.
remove item from stack.
stack contains B, C, D
size is 3
execute loop increment: i is now 1
test loop condition: 1 is smaller than 3 + 1, so we enter the loop...
Finish it off.
OK, now that you understand the problem, how are you going to fix it? The two standard ways to do this are: (1) count first, save the result in a local, test against the local, and (2) remove the counter altogether, since it is unnecessary; instead, loop while the size is greater than zero. I prefer the latter solution; the fewer variables you mutate, the fewer mistakes you'll make in mutating them.
Upvotes: 11
Reputation: 884
It might have to do with the fact that your evaluation expression (i < sStack.size() + 1) is eveluated every iteration. Your stack shrinks every time while 'i' increments every time. You might want to save the stack's size in a variable and compare to that.
Ah, beaten to it by none other than Eric Lippert. I salute you, sir! :)
Upvotes: 0