使用 getText() 时,有没有办法将文本字段中的运算符与数字分开?

Is there a way to seperate operators from number in a textfield when using getText()?

这是完整的代码。

package helwrld;

import javax.swing.*;
import java.awt.event.*;


public class SwingTrial {
    JButton b;
    private double num1;
    private double num2;
    private double result;
    private String opr = "";
    
    
    
    public static void main(String[] args) {
        SwingTrial gui= new SwingTrial();
        gui.go();
    }
    public void go() {
    JFrame box1= new JFrame();
    final JTextField tf=new JTextField(); 
    tf.setBounds(100,50, 150,20); 
    final JButton b= new JButton("1");
    b.setBounds(30,30,60,60);
    final JButton c= new JButton("2");
    c.setBounds(30,100,60,60);
    final JButton d= new JButton("+");
    d.setBounds(30,170,60,60);
    final JButton e= new JButton("=");
    e.setBounds(30,240,60,60);
    final JButton g= new JButton("3");
    g.setBounds(30,310,60,60);
    
    
    
    b.addActionListener(new ActionListener() {// listens for the "1" button being pressed, displays and stores the value in num1
        public void actionPerformed(ActionEvent arg0) {
            
            String eNum= tf.getText()+b.getText();
            tf.setText(eNum);
        }
    });
    
    c.addActionListener(new ActionListener() {// listens for the "2" button being pressed, displays and stores the value in num2
        public void actionPerformed(ActionEvent arg0) {
            
            String eNum= tf.getText()+c.getText();
            tf.setText(eNum);
        }
    });
    
    g.addActionListener(new ActionListener() {// listens for the "2" button being pressed, displays and stores the value in num2
        public void actionPerformed(ActionEvent arg0) {
            
            String eNum= tf.getText()+g.getText();
            tf.setText(eNum);
        }
    });
    d.addActionListener(new ActionListener() {// listens for the "+" button being pressed and displays it
        public void actionPerformed(ActionEvent arg0) {
            opr="+";
            num1= Double.valueOf(tf.getText());
            String eNum= tf.getText()+d.getText();
            tf.setText(eNum);
            
        }
    });
    
    
    e.addActionListener(new ActionListener() { //Adds the sums of the two nums and displays them next to the original numbers.
        public void actionPerformed(ActionEvent arg0) {
            String value= tf.getText();
            char aChar=value.charAt(2);
            num2= Double.valueOf(aChar);
            opr="=";
            if(opr.equals("=")) {
                result= num1+num2;
                
            }
            else if(opr.equals("+")) {
                tf.setText("Can't do that");
            }
            String addAnswer= String.format("%.1f", result); 
            String eNum= tf.getText()+e.getText()+addAnswer;
            
            tf.setText(eNum);
            
        }
    });
    
    
    
    box1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    box1.add(b);
    box1.add(c);
    box1.add(d);
    box1.add(e);
    box1.add(g);
    box1.add(tf);
    box1.setLayout(null);

    box1.setSize(500,500);
    box1.setVisible(true);

    }   
}

起初只是,

String value= tf.getText();
            char aChar=value.charAt(2);
            num2= Double.parseDouble(tf.getText());
            opr="=";
            if(opr.equals("=")) {
                result= num1+num2;
                
            }
            else if(opr.equals("+")) {
                tf.setText("Can't do that");
            }
            String addAnswer= String.format("%.1f", result); 
            String eNum= tf.getText()+e.getText()+addAnswer;
            
            tf.setText(eNum);

但这给了我很多错误。 所以我尝试将字符串值从 getText() 转换为 char,然后是 double,但它给出了错误的答案。

String value= tf.getText();
            char aChar=value.charAt(2);
            num2= Double.valueOf(aChar);
            opr="=";
            if(opr.equals("=")) {
                result= num1+num2;
                
            }
            else if(opr.equals("+")) {
                tf.setText("Can't do that");
            }
            String addAnswer= String.format("%.1f", result); 
            String eNum= tf.getText()+e.getText()+addAnswer;
            
            tf.setText(eNum);

所以我找到了解决方法:

   String value= tf.getText();
   value=String.valueOf(value.charAt(value.length()-1));// gets the last char in _value_
   num2= Double.parseDouble(value);

getText() 输出保存为字符串对我来说看起来有些多余,但这是获得我想要的内容的最舒适方式。

我认为您将不得不考虑偶数位数字 (N > 9)、浮点数和负数 (N < 0)。

我会使用正则表达式在您的字符串中查找可被视为数字的内容。

这是一个例子,但请注意:

  1. 数字需要用空格分隔
  2. 您必须迭代到最后一场比赛才能找到您的号码
public static List<Double> getNumbersFromString(String input) {
    Pattern p = Pattern.compile("([+-]?\d+\.?\d+)|([+-]?\d+)");
    Matcher m = p.matcher(input);
    List<Double> doubleList = new ArrayList<>();
    while (m.find()) {
        doubleList.add(Double.parseDouble(m.group()));
    }
    return doubleList;
}

public static void main(String[] args) {
    String value = "1 + 2 - 30 * 5 / ( 3 + 4.3) + -120"; // tf.getText();

    // My suggestion
    List<Double> numbersFromString = getNumbersFromString(value);
    System.out.println(numbersFromString.get(numbersFromString.size() - 1));

    // Your code
    value = String.valueOf(value.charAt(value.length() - 1)); // gets the last char in _value_
    Double num2 = Double.parseDouble(value);
    System.out.println(num2);
}