2017-04-13 89 views
-1

我的Action Listener中的我的If Else声明无法正常工作。如果其他人不工作?

当按下J按钮时,如果用户输入了一个字符串,其数字为0-9+-*/,则程序正常执行。

否则,JOptionPane会显示一条错误消息。

在下面的代码中,它似乎跳过If的条件,并直接去Else无论是什么?

如果决定编译该代码..
例后缀:11+就等于(1 + 1)时转化为缀

package p2gui; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import java.awt.event.*; 
import javax.swing.JOptionPane; 

/** 
* 
* @author Mike 
*/ 
public class P2GUI extends JFrame implements ActionListener { 

    JFrame f = new JFrame("Three Address Generator");// Title 

    private final JButton evaluate; 
    private final JLabel textfieldLabel; 
    private final JTextField entryField; 
    private final JLabel resutfieldlabel; 
    private final JTextField resultField; 
    private final JOptionPane popup = new JOptionPane(); 

    P2GUI() { 

     f.setSize(425, 180); 
     f.setLayout(null);//using no layout managers 
     f.setVisible(true);//making the frame visible //window size 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     textfieldLabel = new JLabel("Enter Postfix Expression"); 
     f.add(textfieldLabel); 
     textfieldLabel.setBounds(10, 10, 160, 25); 

     entryField = new JTextField(""); 
     //entryField.addActionListener(this);//ActionListener 
     f.add(entryField); 
     entryField.setBounds(160, 10, 220, 25); 

     evaluate = new JButton("Construct Tree"); 

     evaluate.addActionListener(this);//ActionListener 
     f.add(evaluate); 
     evaluate.setBounds(137, 55, 130, 30); 

     resutfieldlabel = new JLabel(" Infix Expression "); 
     f.add(resutfieldlabel); 
     resutfieldlabel.setBounds(20, 100, 100, 25); 

     resultField = new JTextField(""); 
     //resultField.addActionListener(this);//ActionListener 
     resultField.setEditable(false); 
     f.add(resultField); 

     resultField.setBounds(125, 100, 220, 25); 
    } 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       String fullString; 
       fullString = entryField.getText().trim(); 
     if(fullString.matches("\\d+") && fullString.matches("[-+*/]")){ 

      Convert conversion = new Convert(); 
        resultField.setText(conversion.convert(fullString)); 

     } else { 
      JOptionPane.showMessageDialog(null, "Please Enter Digit and 
    Arithmetic operator");   
      //eraseTextField(); 

       } 

      } 

    public void eraseTextField() { 
     entryField.setText(""); 
     entryField.requestFocus(); 
    } 

    public static void main(String[] args) { 
     P2GUI p1GUI; 
     p1GUI = new P2GUI(); 

    } 
} 
/////////////////////////////////////////////END/////////////////////////////////////////////////////////////////////////////// 

隐蔽类

package p2gui; 

import java.util.Stack; 
import javax.swing.JOptionPane; 

/** 
* 
* @author Mike 
*/ 

public class Convert { 

    /** 
    * Checks if the input is operator or not 
    * @param c input to be checked 
    * @return true if operator 
    */ 
private boolean operator(char c){ 
    return c == '+' || c == '-' || c == '*' || c =='/' || c == '^'; 
    } 

    /** 
    * Converts any postfix to infix 
    * @param postfix String expression to be converted 
    * @return String infix expression produced 
    */ 
public String convert(String postfix){ 
    Stack<String> stackIt = new Stack<>(); 

     for (int i = 0; i < postfix.length(); i++) { 
      char c = postfix.charAt(i); 
      if (operator(c)) { 
       String b = stackIt.pop(); 
       String a = stackIt.pop(); 
       stackIt.push("(" + a + c + b + ")"); 
      } else { 
       stackIt.push("" + c); 
      } 
     } 
     return stackIt.pop(); 
    } 
} 
+0

请注意,一个变量只能包含一个值,这是OR和AND运算符中的缺陷 – abcOfJavaAndCPP

+0

@abcOfJava:否,OP正在尝试查明字符串是否包含数字*,并且*包含运算符。 –

回答

0

matches检查是否作为一个整体字符串的正则表达式匹配。如果你想内字符串检查匹配,你需要.*在表达式的两端:

if (fullString.matches(".*\\d+.*") && fullString.matches(".*[-+*/].*")){ 

,它允许用户输入任何东西,只要它至少有一个数字在某处,至少有一个操作员在某处。只要包含这两件东西,他们就可以进入任何他们喜欢的东西。

如果您要检查,他们已经输入的数字和运营商,并已进入了每一个的至少一个:

if (fullString.matches("[-+*/\\d]+") && fullString.matches(".*\\d.*") && fullString.matches(".*[-+*/].*")){ 

,上面写着“只有数字和运营商,以及至少一个数字以及至少一个运营商,以任何顺序。“

我不知道,如果你想将其锁定更多(数字然后操作,或者操作然后数字;只有一个运营商;不允许位,那么运营商,那么更多的数字;等等。 ),但有关if的基本问题是“整个字符串”的事情。

+0

谢谢T.J它现在可以工作。我认为这是非常小的事情 –

0

您正在使用的正则表达式String"\\d+"检查String只有是否包含数字,但是您要检查它是否包含运算符。以下正则表达式应该足够(需要一个数字和一个操作员):

if (fullString.matches(".*\\d+[-+*/]*.*")) { 
    // Code here... 
} 
+0

这需要字符串的数字后跟一个运算符,但它们在'convert'中的代码似乎允许任何顺序(事实上,对于组合)。(不过,我不知道这是否是'convert'或故意的问题。) –

+0

@ T.J.Crowder谢谢,刚编辑它。我需要刷上我的正则表达式! –