How do I convert strings to operators in Java?
                                        
                    •
                    Java                                    
                See English answers > is it possible to pass arithmetic operators to a method in Java? 8
import java.io.*;
import java.math.*;
public class ReadString {
   public static void main (String[] args) {
      System.out.print("Enter the first number: ");
      BufferedReader br = new BufferedReader(new InputStreamReader(system.in)); 
      int numOne = 0 ;
      int numTwo = 0 ;
      String opOne = null;
      while(true){
      try {
          numOne = Integer.valueOf(br.readLine());
          break;
      } catch (IOException error) {
         System.out.println("error; try again.");
         System.exit(1);
      }
      catch (NumberFormatException nfe) {
          System.out.println("error;try again.");
      }
      }
      System.out.print("Enter the second number: ");
      while(true){
      try {
          numTwo = Integer.valueOf(br.readLine());
          break;
       } catch (IOException error2) {
          System.out.println("error");
          System.exit(1);
       } catch (NumberFormatException nfe) {
          System.out.println("error;try again.");
       }
      }
      System.out.println("What would you like to do with " + numOne + " and " + numTwo + "?");
      try {
          operator = br.readLine();
       } catch (IOException ioe) {
          System.out.println("error");
          System.exit(1);
       } catch (NumberFormatException nfe) {
          System.out.println("error");
       } 
   }
}
Solution
The simplest way to do this is the sequence of if then else statements:
if ("+".equals(opOne)) {
    res = numOne + numTwo;
} else if ("-".equals(opOne)) {
    res = numOne - numTwo;
} ...
An advanced method is to define an interface for the operator and put the instance in the map container:
interface Operation {
    int calculate(int a,int b);
}
static final Map<String,Operation> opByName = new HashMap<String,Operation>();
static {
    opByName.put("+",new Operation() {
        public int calculate(int a,int b) {
            return a+b;
        }
    });
    opByName.put("-",int b) {
            return a-b;
        }
    });
    opByName.put("*",int b) {
            return a*b;
        }
    });
    opByName.put("/",int b) {
            return a/b;
        }
    });
}
Using this initialized map, you can perform the following calculations:
int res = opByName.get(opOne).calculate(numOne,numTwo);
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        