Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The program is compiling and running successfully but the only problem is that the output is printed

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


[Edit: put code between [code] and [/code] ubb code blocks]
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal,

First of all please put your source within the code tags.
It isn't clear what your problem is. Maybe it is that your output is not being printed.Is it?
But that should be least of your worries,as there is one major mistake in your array initialization for the stack.

your code says :


now when you instantiate the object of type stack, maximum_stacks field is initialized to its default value of 0, hence your array is of length 0. Constructor later just changes the value of maximum_stacks without affecting the size of the stack.

To correct it you must code it as :


Why are you printing the value of pushed values in a different loop, why not include it in the same loop where you are actually pushing the values.
I haven't compiled the new code but I think it should work fine, unless there are some more errors too.

I hope I'm right.


 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, I don't think logic of your pop() method is correct.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnks deepak i am just a beginner so i just need to improvise my logic and you were right about my problem and you helped me understanding the logic..thnks

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i got the output as follows:
pushing values now...
popping values now...

the actual output iam expecting is
pushing values now...

Pushed values-->0
Pushed values-->1
Pushed values-->2
Pushed values-->3
Pushed values-->4
.
.
.Pushed values-->9

popping values now...
9
8
7
6
5
4
3
2
1
0




 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is a new pop() which might help. Note that I made it to so that it looks and functions like your code. This not how I would code a stack pop() method, or you should for that matter.



I am using topOfStack to point to , obviously, to the top of the stack.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also a newbie ( started nearly a week back) , but this is how I would code them:



Someone experienced please comment.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you use dis kind of code
void push(int data){
stack_data[++topOfStack]= data;
}

int pop(){
return stack_data[topOfStack--];
}

isnt it necessary to use for loop..???

 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you would need to call push and pop in a loop.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have the ++ and -- the wrong way round, Deepak. Surely it's topOfStack++ and --topOfStack, if you start with topOfStack = 0? And it might be better to call that variable size or count.
Agree if you want to read all the data you would want the push() call and the pop() call inside loops. I would actually set up an isEmpty() method, then you can say
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I initialized topOfStack=-1 in the class declaration.
'-1' would indicate an empty stack, which can be checked in an isEmpty() method. This isEmpty() method 'should' be called before popping the values from stack, or else we might run into ArrayIndexOutOfBoundsException exception.
Now I think the ++ & -- should work perfectly fine.
I hope I'm going the right way Ritchie.


 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better to call "topOfStack" size, then it starts at 0, then the size and the array index are always the same when adding. The size is always 1 more than the array index when removing. Don't start with 1; I think you will get null returns or even ArrayIndexOutOfBoundsExceptions if you start at 1.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody tell me the reason why my 1st (previous) program was compiling and running but the popped in values and pushed values were not displayed

 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I answered it in the first post itself.
That correction will allow you to see pushed values printed

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic