aspose file tools*
The moose likes Beginning Java and the fly likes The program is compiling and running successfully but the only problem is that the output is printed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The program is compiling and running successfully but the only problem is that the output is printed" Watch "The program is compiling and running successfully but the only problem is that the output is printed" New topic
Author

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

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056



[Edit: put code between [code] and [/code] ubb code blocks]


http://www.lifesbizzare.blogspot.com || OCJP:81%
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
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

Joined: Jul 28, 2009
Posts: 45
BTW, I don't think logic of your pop() method is correct.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

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

Joined: Aug 01, 2009
Posts: 1056

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

Joined: Jul 28, 2009
Posts: 45
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

Joined: Jul 28, 2009
Posts: 45
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

Joined: Aug 01, 2009
Posts: 1056

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

Joined: Jul 28, 2009
Posts: 45
you would need to call push and pop in a loop.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
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

Joined: Jul 28, 2009
Posts: 45
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

Joined: Oct 13, 2005
Posts: 39884
    
  28
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

Joined: Aug 01, 2009
Posts: 1056

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

Joined: Jul 28, 2009
Posts: 45
I answered it in the first post itself.
That correction will allow you to see pushed values printed

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: The program is compiling and running successfully but the only problem is that the output is printed