Need help with java program

duragezic

Lifer
Oct 11, 1999
11,234
4
81
I've been working on this assignment for over a week now, and it's due this Friday. I need to make a class Evaluator that will take a string and perform two methods on it: getExpression(), which is to simply return the string with any spaces and preceding zeroes removed, and it will put negative values in parentheses. I haven't gotten the getExpression() quite done yet, but it shouldn't be too hard.

The other method is reduce(), which will perform the next operation in the expression. So after getExpression() is called and for example returns: "1*6+30", then I would want to evaluate the 1*6 first, then getExpression() would be called again to verify it now reads "6+30", then call reduce() again so that it will evaluate the addition part.

My prof's hints says that we should split the expression into two arrays; one int array of the values, and one char array of the operators. I've done that. Then when reduce() is called, I searched the operators array for a multiplication, saved its index, and replaced the corresponding value in the values array with values[index] * values[index+1]. But now, according to his hints, we must delete the operator from the operator array and move all following opreators down one position, and after replacing the first value with the result of the operation, I must move all the following values down one position. How would I go about doing this? I was able to move all the values down one position, except that the last position would get duplicated. So in essence, I would like to shrink the size of the array, even though I know you can't (and I initialized both the values array and operators array with room for 20)

edit: Well I should clarify that currently when I move all of the following values down, what used to be the last value in the array is now a zero. When the int array is created, it is given all zeroes by default. I suppose I could put an if statement to determine if what follows is a zero then don't display it, but how would I differentiate between zeroes which are intentional (such as 1+200) and which are just unused spaces in the array that by default are zero.

As I add operators and values to their arrays, I have a counter increment with each addition, so that I have two int variables that will indicate the "filled" size of the array.

edit 2: So do you think I could get by with decrementing my counters to sort of fake that the array is getting smaller, but really I'm just iterating to one less after each reduce().
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
There are different ways of doing this.

If you have to stick to arrays, then keep separate counter variable. So, when you remove something for the array, decrement the counter. If you need to know how many valid cells in your array or if the array is empty, then reference the counter. Otherwise, you can run into weird situations as you described above.

Edit: Sorry, I just saw your edits. You just described what I was suggesting. Also, there really isn't anything "fake" about it. It's a real way of dealing with statically allocated data structures, such as an array.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
If the arrays are just a hint and not an order, I'd be looking at using an ArrayList<Integer> or LinkedList<Integer>.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
char[] smaller = new char[bigger.length - 1];
for(int ii = 0; ii < smaller.length; ii++){
smaller[ii] = bigger[ii+1];
}
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: notfred
char[] smaller = new char[bigger.length - 1];
for(int ii = 0; ii < smaller.length; ii++){
smaller[ii] = bigger[ii+1];
}
So he's going to do that over and over again until it's down to one entry? The Collections Framework was designed to save you from just such work.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Ok sorry but my original question isn't really the problem anymore. Thanks though, it looks like these counters I decided to use when I began are more useful that I thought they would be!

kamper: Yeah isn't that something like the Vector class? We've never done such a thing in class, so I'm not sure if I could implement it correctly, although looking at the API now, it looks fairly straight-forward... but I'm a real noob with this stuff.

notfred: Correct me if I'm wrong, but the problem I discovered while trying something like that was bigger.length would return the length of the array (which I chose to be 20 since the expressions will never be larger than that), even though it isn't really full since I'm testing with expressions that only have a few values and operators. I have an if statement to skip it if is determined to be a ' ' char.

My problem right now, my getExpression() method isn't working correctly because it is suppose to put any negative values into parentheses, so if it was sent "1-4*8" it should return "1+(-4)*8". However, mine returns "1+(-4)4*8 1+(-4)4*8". So, this tends to mess up when it goes to evaluate.

My code for getExpression() is as follows:
(int valsCounter is # of values in the array, operators[] is my char array of ops, values[] is my int array of values, and expression is what will be returned and printed to the screen)
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Try this:

EDIT: N/m I can see right away after looking at it again that it wouldn't work. Booo