2017-04-13 84 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謝謝,剛編輯它。我需要刷上我的正則表達式! –