File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Compare operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compare operators" Watch "Compare operators" New topic

Compare operators

Torvald Helmer

Joined: Oct 26, 2005
Posts: 8
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

Joined: Aug 16, 2005
Posts: 15092

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):

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

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
Torvald Helmer

Joined: Oct 26, 2005
Posts: 8
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

Joined: Aug 20, 2001
Posts: 1824

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:

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Torvald Helmer

Joined: Oct 26, 2005
Posts: 8
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(

line 40 is: infixPri = priorities.get(infixOp);
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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:

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Compare operators
It's not a secret anymore!