aspose file tools*
The moose likes Beginning Java and the fly likes stack Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "stack" Watch "stack" New topic
Author

stack

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok i have a program to do, that is supposed to take and expression from a user and calculate it, and tell what it equals, or if it is not a valid expression.

and by an expression i mean for example:

(1 + (3 * ( 5 - 4 )))

ok here is my question...

how do i get the scanner to scan each individual char/num.

do i take it as a string, and search it for char or numbers?

because in the program i have to put the numbers in one stack and the

operators in another.

but when or if i do have to do a loop to search the string

"( 1 + ( 3 * ( 5 - 4 )))"

can i convert the char that is actually a number, into an integer?

and after i do this and get all the nums, and ops in their stacks, the

algorithm says to pop the first two in the number stack and the first

operator in that stack, then solve that, then push that found value in the

number stack.

and can i do, for instance?



thanks,
Justin..


You down with OOP? Yeah you know me!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
and also, he wants the ^ to basically equal like, 3^2 = 9;

how do i do that if the user is entering a expression of their choice?

nevermind, i under stand, but how do i make ^ = to java's version of how to

raise a number to a certain power?

which is Math.pow(int,int);

could i do?



thx for replying...

Justin
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
but wait, maybe i should do this first


because if i put the expression in a stack, its going to come out in reverse.

so i need to evaluate each small expression by reversing the .pop()'s.

thanks,

Justin
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
here is some of my code....

and i know i could've switched, but i was lazy.


thanks for the replies...

Justin
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok, my proffesor said we have to create our own stack driver class...

but doesn't java already have a predefined stack class with the following methods?

push();
pop();
isNotEmpty();
isNotFull();
peek();
dump();

these are already ready for you to use right?...then why make another

implementation of stack when it already exists...

oh and also, like say a user inputs the following at the prompt.


is there away where i can scan each individual character, switch the numerical values (0 - 9), and the operators(+,-,*,/,%,^).

and at the same time be loaded them into two different stacks??

oh! and i have another important question..

can i create my class as follows?
or is it legal, is a more reasonable question.



because i want to create a stack class, but NOT! two different ones, one to hold a char, and one to hold a int.

and another thing, IF i wanted to compare a char to an int could i do the following?




thanks,
Justin
[ June 06, 2006: Message edited by: Justin Fox ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, I think we're tackling several problems at once and need to focus on one.

Yes, Java has a Stack class. See the JavaDoc and find Stack in the left class list to see the methods. It's up to your prof whether he wants you to use that one or make the problem a little harder and write your own.

If you have to write your own, try to write a MyStack that will make this code say "true". You can put this in the main() of a class called Tester or something.

Only after that works, make these work:

Then add these lines:

Note that this stack only works with Strings, so you'll have to use String types for parameters and return values. If you get that working we can talk about how to use ints and chars.

Sound fun?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok here is the stack driver, i can't seem to figure out how to define the pop() method



thanks,
Justin
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That looks pretty close. An array-based stack is pretty fast and simple, but has a fixed size as you set it up in your constructor. It will do the job for a school assignment with reasonable limits. After this class, use the one in the JDK library.

You want pop() to return the most recently pushed value. What index do you have that points to the most recently pushed entry? What do you want to do to that index before you return? Think about calling push() three times then pop() three times. Draw the array and track the index on paper if that helps.

Your size() returns the capacity of the array. (And there is a much easier way to get that from an array.) Is that what you want? Or the number of entries currently in the stack? Which index will tell you that?

Your push() fails silently if the stack is full. Think about what it might do to indicate an error instead.

Does this compile? peek() is using an "i" that I don't see defined. I think you want that same index again here!

Translate the little set of tests I suggested into ints instead of strings and see how your stack class works. You'll have it in no time. Keep having fun!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok i modified the driver class, and implemented a main class....

but when i call dump, it calls it infinitely..

and i can't figure out why...



and i have a main class too...

i dont understand why you return 0 in the pop and push function, why not just make the void and not return anything?.. all you want to do is take the value at top out and the make top point to top--, but how do you remove a value from a place in an array, i know you could say array[top] = 0;

but can you say array[top] = null;

or would it give a null pointer exception?

and in the dump method...

i have it print out

"top: Contents: "

but when i call dump all it does is print..

top: 0 contents:

over and over again..

but i didn't call the function recursivley ... and i said...oh wait..

i need to have i ++ lol in the while loop

im gay, ok thanks, but try and help me answer my other questions...

thx
Justin

oh and here is my main class..

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
if i switch variable containing a char value,

do i need to do this?

switch(value)
{
case '0':
NumStack.push(0);
break;
}

??

thanks,
Justin
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490


Why is this an infinite loop? Look at the exit condition. This loop is either going to run 0 times, or just keep running.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
is there anyway to convert a char to an int in java?

i tried

char value = charArray[i];
NumStack.push((int)value);

plz help me!!
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
int value = charArray[i];//no cast necessary because char is a subset of int
NumStack.push(value);

Why not just store them as chars? Or just immediately convert the input to an int?
[ June 08, 2006: Message edited by: Rusty Shackleford ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok, i have the other crap taken care of...

but when it picks up the char '^' for example:

2^2 = 4, but the prof doesnt want us using math.pow, because its only

for exponets that are non integer...

i tried a recursive method, but im stuck...

here is my code so far

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
If you want to pass a value recursively(or a non-recursive method call for that matter), then add it to the recursive call. It is just this simple:

return recExp(num1,num2-1);

If you change the value somewhere in the method, then of course num1 will not be its original value, but you should have an idea how to do it in that case. Since you never modify num1, this is not an issue in this case.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stack