• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

converting this to a prefix instead of a postfix calculator

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tell me am I close ?
IF I move the getEquation to under the

would that make a difference?
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now I am back to this error again:



how do I get past this....what concpet am I missing here?
/tmp/28454/PolishNotationCalculator.java:23: non-static variable getEquation cannot be referenced from a static context
PolishNotationCalculator calculator = new PolishNotationCalculator (getToken (getEquation));
^
1 error
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this actually compiled....test to see if it will work...
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it compiled and somewhat ran but when I enter + 3 4 I got an No Such element error....any reasons why..I think it has to do with the reuten of the empty array but when I added the static to the String getEquation it required me to put a return statement in the method....

Any suggestions?
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[code]
import javax.swing.JOptionPane;
import java.util.*;

public class PolishNotationCalculator
{
public static void main (String [] args)
{

do
{
try
{

}
catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "You must enter a number for the operand", "Input Error",

JOptionPane.ERROR_MESSAGE);
}
catch (NoSuchElementException mistake)
{
JOptionPane.showMessageDialog(null, "You must enter an equation to be evaluated", "Input Error",

JOptionPane.ERROR_MESSAGE);
}
PolishNotationCalculator calculator = new PolishNotationCalculator (taxpayer.getTaxpayer());

int result = calculator.evaluate();
System.out.println(result);
}
while (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "Do you want to enter another?", "Enter Scores",

JOptionPane.YES_NO_OPTION));
}
String getEquation = JOptionPane.showInputDialog(null, "Please enter an equation to be evaluated using Polish

Notation \n" + "i.e enter +, -, /, *, first and then two numbers \n"+ "example + 3 4 for 3 + 4", "Polish

Calculator",JOptionPane.QUESTION_MESSAGE);


private String [] taxpayer;
private int index;

public PolishNotationCalculator (String [] taxpayer)

{
setTaxpayer(taxpayer);
index = 0;
}

// get methods

public String [] getTaxpayer ()
{
StringTokenizer tok = new StringTokenizer(getEquation);

int length = getEquation.length();
String [] taxpayer = new String [length];
while (tok.hasMoreTokens())
{
int x = 0;
do
{
taxpayer[x] = tok.nextToken();
++x;
}
while (x < length);
return taxpayer;
}

//set methods

public void setTaxpayer ( String [] taxpayer)
{
taxpayer = taxpayer;
}

public int evaluate ()
{
int retval = 0;
if ("+".equals(taxpayer[index]))
{
++index;
int op1 = evaluate();
++index;
int op2 = evaluate();
retval = op1 + op2;
}
else if ("-".equals(taxpayer[index]))
{
++index;
int op1 = evaluate();
++index;
int op2 = evaluate();
retval = op1 - op2;
}
else if ("x".equals(taxpayer[index]))
{
++index;
int op1 = evaluate();
++index;
int op2 = evaluate();
retval = op1 * op2;
}
else if ("/".equals(taxpayer[index]))
{
++index;
int op1 = evaluate();
++index;
int op2 = evaluate();
retval = op1 / op2;
}



else
{
retval = Integer.parseInt(taxpayer[index]);
}
return retval;
}


}
[/code}

Why am I getting these errors:

/tmp/6417/PolishNotationCalculator.java:65: illegal start of expression
public void setTaxpayer ( String [] taxpayer)
^

I promise you once I finished with this class I won;t be brothering any of you again. I am so done with Java.

Please help.
Bradley
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's definitely caused by the empty array...



Basically, what you are doing is... Assuming that response will have as many tokens as the size of the string. So the only way you'll return any values is when you fill the taxpayer array. This is clearly wrong as there is no way that there will be the number of tokens as there are characters in the equation string.

Anyway, the while loop will always terminate before you can fill the array, because you will run out of tokens. Then you simply declare an empty array for processing.

Henry
 
Henry Wong
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I promise you once I finished with this class I won't be brothering any of you again. I am so done with Java.



Java is, IMHO, one of the easier object oriented languages to learn... I suggest you give it another chance, unless you mean "so done with" computer programming as a possible profession.

As for your question...

/tmp/6417/PolishNotationCalculator.java:65: illegal start of expression
public void setTaxpayer ( String [] taxpayer)
^



This is caused by your previous method. You are missing some close brace from getTaxpayer(). The compiler thinks you are trying to declare the setTaxpayer() method within the getTaxpayer() method.

Henry
[ March 07, 2007: Message edited by: Henry Wong ]
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like Visual Basic a lot but that is no way near as complex. I wanted to be a programmer but I am struggling mightly with this. I am only have till trmw night at 6. My work uses C# but it wasn;t offered so I had to take Java.

So if my array mehtod isn't waorking, I am back to square one. I am wondering if I can just create a large array and not worry about the size and just plug the tokens into it. That will work because this is small but Iknow in bigger programs not as well.

would it work without the while statement?
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also no matter what variable I plug into the
PolishNotationCalculator calculator = new PolishNotationCalculator (taxpayer.getTaxpayer());

I will get this error /tmp/30699/PolishNotationCalculator.java:23: non-static variable taxpayer cannot be referenced from a static context
PolishNotationCalculator calculator = new PolishNotationCalculator (taxpayer.getTaxpayer());
^
/tmp/30699/PolishNotationCalculator.java:23: cannot resolve symbol
symbol : method getTaxpayer ()
location: class java.lang.String[]
PolishNotationCalculator calculator = new PolishNotationCalculator (taxpayer.getTaxpayer());
^
but if I leave it blank I get this:

/tmp/2583/PolishNotationCalculator.java:23: PolishNotationCalculator(java.lang.String[]) in PolishNotationCalculator cannot be applied to ()
PolishNotationCalculator calculator = new PolishNotationCalculator ();
^
1 error

I am compelty lost now....
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I could create a new class and jsut bring everything in from that through the

PolishNotationCalculator calculator = new PolishNotationCalculator ();

would that work?
 
Henry Wong
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I like Visual Basic a lot but that is no way near as complex. I wanted to be a programmer but I am struggling mightly with this. I am only have till trmw night at 6. My work uses C# but it wasn;t offered so I had to take Java.



C# is, IMHO, probably the other easy object oriented language to learn. Visual Basic is ... well, I use it for very small programs. (And many times, I prefer to use WBEM instead) It gets way too messy to use for anything large.

So if my array mehtod isn't waorking, I am back to square one. I am wondering if I can just create a large array and not worry about the size and just plug the tokens into it. That will work because this is small but Iknow in bigger programs not as well.

would it work without the while statement?



Yea... it looks like you grabbed code from two different locations, and slammed it together. Getting rid of the while loop is probably the easier loop to get rid of. You can get rid of the empty array stuff too.

BTW, for your array length, you could also use tok.countTokens() instead, which returns the number of tokens for the taxpayer array. With the array at the right size, then you probably won't be encountering the "no such element" problem.

Henry
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Praise the lord!!! I got it working...here is the final code:


Thank you for all your help guys....Made my night. I have about 24 hours to spare.
 
Henry Wong
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great !!! ...

BTW, the Java compiler is pretty good at working with multiple lines, such as:



When you break code into multiple lines, depending on the editor, it may be much more readable.

Henry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic