• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compare operators

 
Torvald Helmer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to compare two operators.
The operators are: + - * / ^
They have all different priority
^ = 3
* = 2
/ = 2
+ = 1
- = 1

How can I compare a character '*' with
a character '+' of which one has the
highest priority?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw your question in the forums at Sun's website too.

First of all, you need to get exactly clear what you want. You have two strings containing two operators, for example "*" and "+". Now what output exactly do you want?

Do you just want to know which of the two has the highest priority, for example?

You have to write a piece of code that contains a data structure to keep track of the priorities of the different operators, and then you need to lookup the priorities of the two you want to compare. For the data structure, try a HashMap. For example (uses Java 5.0 features):
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd probably try a map or a lookup of some kind so I could say:

Does that look like it would work? This is a problem solved in every CS program in the world, so somebody has probably found a slicker way to do it by now.
 
Torvald Helmer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong!!!

I thought this really would work, but
surely I have missed something.

This is my code:



At these lines: "stack.push(String.valueOf(c));"
I get this message -->

Type safety: The method push(Object) belongs to the raw type Stack. References to generic type
Stack<E> should be parameterized
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're getting that because you haven't declared the type of object that your stack holds. Note that that should just be a warning, not an error.

To fix it, you would declare your stack:
 
Torvald Helmer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Joel McNary!!

Now that problem is out of the way.

But there's still something that messes things up.

When I cmopile the code I get this:
______________
Exception in thread "main" java.lang.NullPointerException
at InfixToPostfix.main(InfixToPostfix.java:40)
______________

line 40 is: infixPri = priorities.get(infixOp);
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your "for" loop loops over every character in "infixStr", but the HashMap only contains entries for a few characters. When the loop hits a character not in the Map, you'll see this exception. So you need to check whether a given character is an operator or not, first. You could just split this line into two and not use auto-unboxing:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic