Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## RPM Calculation

I have a program that I wrote a long time ago that converts an algebraic expression in RPM. How would I go about doing the actual calculation? Here is the source

```import java.util.Stack;
import java.io.*;
import javax.swing.*;

public class RPN{
public static boolean precedence(char a,char b){
int pos[]=new int[2];
pos[0]=-1;
pos[1]=-1;
for(int i=0;i<s.length;i++){
if(s[i].indexOf(b)==1 && s[i].indexOf(a)!=0) return false;
}
for(int i=0;i<oper.length;i++){
if(oper[i].indexOf(a)>-1) pos[0]=i;
if(oper[i].indexOf(b)>-1) pos[1]=i;
}
if(pos[0]<pos[1]) return true;
else if(pos[0]>=pos[1] && pos[0]!=-1 && pos[1]!=-1) return false;
return true;
}

public static String prepare(String infix){
infix=infix.replaceAll("[ ]+","");
for(int i=0;i<oper.length;i++){
for(int k=0;k<oper[i].length();k++){
infix=infix.replaceAll("\\"+oper[i].charAt(k)," \\"+oper[i].charAt(k)+" ");
}
}

for(int i=0;i<s.length;i++){
for(int k=0;k<s[i].length();k++){
infix=infix.replaceAll("\\"+s[i].charAt(k)," \\"+s[i].charAt(k)+" ");
}
}

infix=infix.replaceAll("[ ]+"," ");
if(infix.charAt(0)==' ') infix=infix.replaceFirst(" ","");
return infix;
}

public static String toPrefix(String infix){
infix=prepare(infix);
Stack<Character> op_stack=new Stack();
String[] token=infix.split(" ");
StringBuffer out=new StringBuffer();

for(int i=0;i<token.length;i++){
boolean operator=false;
for(int k=0;k<oper.length;k++){
if(oper[k].indexOf(token[i].charAt(0))>-1) operator=true;
}
for(int k=0;k<s.length;k++){
if(s[k].indexOf(token[i].charAt(0))>-1) operator=true;
}

if(operator==false){
out.append(token[i]);
out.append(" ");
}
else{
boolean empty=op_stack.isEmpty();
char op=token[i].charAt(0);
char peek;
boolean operand=false;

while(!(empty=op_stack.isEmpty()) && (!precedence(peek=op_stack.peek(),op))){

out.append(op_stack.pop());
out.append(" ");
}

if(!(empty=op_stack.isEmpty())){
for(int k=0;k<s.length;k++){
if(s[k].indexOf(op_stack.peek())==0 && s[k].indexOf(op)==1) operand=true;
}
}

if(empty || operand==false)
op_stack.push(op);
else op_stack.pop();
}
}
while(!op_stack.isEmpty()){
out.append(op_stack.pop());
out.append(" ");
}
return out.toString();
}

public static void main(String[] args){
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane Pane=new JOptionPane();
frame.setContentPane(Pane);
String infix=Pane.showInputDialog("Enter String");
Pane.showMessageDialog(frame,toPrefix(infix),infix,JOptionPane.ERROR_MESSAGE);
frame.dispose();
}

private static final String [] oper={"+-","*/","^"};
private static final String [] s={"()","[]","{}"};
}```

2. ## Re: RPM Calculation

How would I go about doing the actual calculation?
Can you explain in more detail what you want to do?
The posted code has NO comments explaining what it is doing.
Is RPM revoltions per minute?

3. ## Re: RPM Calculation

I think you mean RPN (Reverse polish notation)?

Take a look at Wikipedia:Reverse Polish Notation for a simple algorithm for evaluating RPN expressions.