Java 如何使它只接受第一个文本字段中的字符串?

Java 如何使它只接受第一个文本字段中的字符串?,java,string,applet,int,textfield,Java,String,Applet,Int,Textfield,我的代码是用于展开二项式的小程序的。我还没有完成它,但它只接受1到10之间的指数,包括1和10。我到了7点,但就像我说的,我还没做完。我的问题是:在第一个文本字段中,它将接受一个数字和一个变量(如4a或其他)。我写的代码不能考虑这个数字(它应该只接受一个变量)。那么…我如何得到它,使第一个文本字段不允许有一个int?谢谢 import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.app

我的代码是用于展开二项式的小程序的。我还没有完成它,但它只接受1到10之间的指数,包括1和10。我到了7点,但就像我说的,我还没做完。我的问题是:在第一个文本字段中,它将接受一个数字和一个变量(如4a或其他)。我写的代码不能考虑这个数字(它应该只接受一个变量)。那么…我如何得到它,使第一个文本字段不允许有一个int?谢谢

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*; 
import java.applet.*; 
import javax.swing.*; 
import java.text.DecimalFormat; 
import java.util.ArrayList; 
import javax.swing.Action;

public class BinomialExpander extends JApplet implements ActionListener
{
  JLabel welcome;
  JLabel directions;
  JLabel example;

  JLabel instructions;
  JLabel startOfBinomial;
  JLabel plusSign;
  JLabel forExponent;
  JLabel endOfBinomial;

  JTextField firstVar;
  JTextField txtSecond;
  JTextField exp;


  JLabel lblExpanded; 
  JLabel outputExpanded;  
  FlowLayout layout;

  JButton compute;

  private int[] pascal1 = {1,1};
  private int[] pascal2 = {1,2,1};
  private int[] pascal3 = {1,3,3,1};
  private int[] pascal4 = {1,4,6,4,1};
  private int[] pascal5 = {1,5,10,10,5,1};
  private int[] pascal6 = {1,6,15,20,15,6,1};
  private int[] pascal7 = {1,7,21,35,35,21,7,1};
  private int[] pascal8 = {1,8,28,56,70,56,28,8,1};
  private int[] pascal9 = {1,9,36,84,126,84,36,9,1};
  private int[] pascal10 = {1,10,45,120,210,120,45,10,1};

  private String a;
  private int y; 
  private int expo;
  private String x;

  public void init() 
  {
      Container c = getContentPane(); 
      c.setBackground(Color.pink);

      layout = new FlowLayout();
      layout.setAlignment(FlowLayout.LEFT);
      c.setLayout(layout);
      setSize(500,175);

      welcome = new JLabel("Welcome to the Binomial Expander Applet!");
      welcome.setFont(new Font("Copperplate Gothic Bold", Font.PLAIN, 16));
      directions = new JLabel("Enter binomial ONLY in the form: (VARIABLE + NUMBER)^EXPONENT");
      directions.setFont(new Font("Times New Roman", Font.PLAIN, 14));
      example = new JLabel("EXAMPLE: (x + 2)^2.");
      instructions = new JLabel("Enter the variable of your binomial:");
      startOfBinomial = new JLabel(" (");
      firstVar = new JTextField(4);
      plusSign = new JLabel(" + ");
      txtSecond = new JTextField(4);
      endOfBinomial = new JLabel(")");
      forExponent = new JLabel("^");
      forExponent.setFont(new Font("Times New Roman", Font.PLAIN, 10));
      exp = new JTextField(2);
      compute = new JButton("Compute!");
      compute.addActionListener(this);
      lblExpanded = new JLabel("Your expanded binomial is: ");
      outputExpanded = new JLabel("");
      c.add(welcome);
      c.add(directions);
      c.add(example);
      c.add(instructions);
      c.add(startOfBinomial);
      c.add(firstVar);
      c.add(plusSign);
      c.add(txtSecond);
      c.add(endOfBinomial);
      c.add(forExponent);
      c.add(exp);
      c.add(compute);
      c.add(lblExpanded);
      c.add(outputExpanded);
  }
  public void actionPerformed(ActionEvent event) 
  {
      a = firstVar.getText();
      y = Integer.parseInt(txtSecond.getText());
      expo = Integer.parseInt(exp.getText());

      outputExpanded.setText(expand()); 
  }
  public String expand()
  {  
     if (expo < 1 || expo > 10)
     {
        x = "ERROR: Exponent value must be between 1 and 10, inclusive.";
        return x;
     }
     else if (expo == 1)
     {
        x = a + "+" + y;
        return x;  
     }
     else if (expo == 2)
     {
        x = a + "^" + expo + " + " + (pascal2[1] * y) + a + " + " + (pascal2[2] * Math.pow(y, expo));
        return x;
     }
     else if (expo == 3)
     {
        x = a + "^" + expo + " + " + (pascal3[1] * y) + a + "^" + (expo-1) + (pascal3[2] * Math.pow(y, expo-1)) + a + " + " + (Math.pow(y, expo));
        return x;
     }
     else if (expo == 4)
     {
        x = a + "^" + expo + " + " + (pascal4[1] * y) + a + "^" + (expo-1) + " + " + (pascal4[2] * Math.pow(y, expo-2)) + a + " ^ " + (expo-2) + " + " + (pascal4[3] * 
        Math.pow(y, expo-1)) + a + "+" + (Math.pow(y,expo));
        return x;
     }
     else if (expo == 5)
     {
        x = a + "^" + expo + " + " + (pascal5[1] * y) + a + "^" + (expo-1) + " + " + (pascal5[2] * Math.pow(y,expo-3)) + a + "^" + (expo-2) + " + " + (pascal5[3] * 
        Math.pow(y, expo-2)) + a + "^" + (expo-3) + " + " + (pascal5[4] * Math.pow(y, expo-1)) + a + " + " + (Math.pow(y, expo));
        return x;
     }
     else if (expo == 6)
     {
        x = a + "^" + expo + " + " + (pascal6[1] * y) + a + "^" + (expo-1) + " + " + (pascal6[2] * Math.pow(y, expo-4)) + a + "^" + (expo-2) + " + " + (pascal6[3] *
        Math.pow(y, expo-3)) + a + "^" + (expo-3) + " + " + (pascal6[4] * Math.pow(y, expo-2)) + a + "^" + (expo-4) + " + " + (pascal6[5] * Math.pow(y, expo-1)) +
        a + " + " + (Math.pow(y,expo));
        return x;
     }
     else if (expo == 7)
     {
        x = a + "^" + expo + " + " + (pascal7[1] * y) + a + "^" + (expo-1) + " + " + (pascal7[2] * Math.pow(y, expo-5)) + a + "^" + (expo-2) + " + " + (pascal7[3]
        * Math.pow(y, expo-4)) + a + "^" + (expo-3) + " + " + (pascal7[4] * Math.pow(y, expo-3)) + a + "^" + (expo-4) + " + " + (pascal7[5] * Math.pow(y, expo-2))
        + a + "^" + (expo-5) + (pascal7[6] * Math.pow(y, expo-1)) + a + " + " + (Math.pow(y, expo));
        return x;
     }
     return x; 
  }
  }
import java.applet.applet;
导入java.awt.*;
导入java.awt.event.*;
导入java.applet.*;
导入javax.swing.*;
导入java.text.DecimalFormat;
导入java.util.ArrayList;
导入javax.swing.Action;
公共类BinomialExpander扩展JApplet实现ActionListener
{
欢迎你;
JLabel方向;
JLabel示例;
JLabel指令;
JLabel开始二项式;
JLabel加符号;
JLabel前指;
JLabel内二项式;
JTextField-firstVar;
JTextField-txtsond;
JTextField-exp;
JLabel-lblExpanded;
JLabel输出扩展;
流程布局;
JButton计算;
私有int[]pascal1={1,1};
私有int[]pascal2={1,2,1};
私有int[]pascal3={1,3,3,1};
私有int[]pascal4={1,4,6,4,1};
私有int[]pascal5={1,5,10,10,5,1};
私有int[]pascal6={1,6,15,20,15,6,1};
private int[]pascal7={1,7,21,35,35,21,7,1};
private int[]pascal8={1,8,28,56,70,56,28,8,1};
private int[]pascal9={1,9,36,84126,84,36,9,1};
私有int[]pascal10={1,10,45120210120,45,10,1};
私人字符串a;
私营企业;
私人国际展览;
私有字符串x;
公共void init()
{
容器c=getContentPane();
c、 挫折背景(颜色:粉红色);
布局=新的FlowLayout();
布局.设置对齐(FlowLayout.左);
c、 设置布局(布局);
设置大小(500175);
欢迎=新的JLabel(“欢迎使用二项式扩展器小程序!”);
欢迎使用.setFont(新字体(“铜版哥特式粗体”,Font.PLAIN,16));
方向=新的JLabel(“仅以以下形式输入二项式:(变量+数字)^指数”);
方向.setFont(新字体(“Times new Roman”,Font.PLAIN,14));
示例=新的JLabel(“示例:(x+2)^2”);
说明=新的JLabel(“输入二项式变量:”);
STARTOFBINOMEL=新的JLabel(“”);
firstVar=新的JTextField(4);
plusSign=新的JLabel(“+”);
txtSecond=新的JTextField(4);
endofbinomential=新的JLabel(“)”;
forExponent=newjlabel(“^”);
setFont(新字体(“Times new Roman”,Font.PLAIN,10));
exp=新的JTextField(2);
compute=新的JButton(“compute!”);
compute.addActionListener(this);
lblExpanded=newjlabel(“扩展的二项式为:”);
outputExpanded=新的JLabel(“”);
c、 添加(欢迎);
c、 添加(指示);
c、 添加(示例);
c、 添加(说明);
c、 添加(开始二项式);
c、 添加(第一个变量);
c、 添加(加号);
c、 添加(txtsond);
c、 添加(内隐二项);
c、 添加(forExponent);
c、 添加(exp);
c、 加(计算);
c、 添加(lblExpanded);
c、 增加(输出扩展);
}
已执行的公共无效操作(操作事件)
{
a=firstVar.getText();
y=Integer.parseInt(txtSecond.getText());
expo=Integer.parseInt(exp.getText());
outputExpanded.setText(expand());
}
公共字符串扩展()
{  
如果(世博会<1 | |世博会>10)
{
x=“错误:指数值必须介于1和10之间,包括1和10。”;
返回x;
}
否则如果(世博会==1)
{
x=a+“+”+y;
返回x;
}
否则如果(世博会==2)
{
x=a+“^”+expo+“+”+(pascal2[1]*y)+a+“+”+(pascal2[2]*Math.pow(y,expo));
返回x;
}
否则如果(世博会==3)
{
x=a+“^”+expo+“+”+(pascal3[1]*y)+a+“^”+(expo-1)+(pascal3[2]*Math.pow(y,expo-1))+a+“+”+(Math.pow(y,expo));
返回x;
}
否则如果(世博会==4)
{
x=a+“^”+expo+“+”+(pascal4[1]*y)+a+“^”+(expo-1)+“+”+”+(pascal4[2]*Math.pow(y,expo-2))+a+“^”+(expo-2)+“+”+(pascal4[3]*
Math.pow(y,expo-1))+a++“+”+(Math.pow(y,expo));
返回x;
}
否则如果(世博会==5)
{
x=a+“^”+expo+“+”+(pascal5[1]*y)+a+“^”+(expo-1)+“+”+”+(pascal5[2]*Math.pow(y,expo-3))+a+“^”+(expo-2)+“+”+(pascal5[3]*
Math.pow(y,expo-2))+a+“^”+(expo-3)+“+”+(pascal5[4]*Math.pow(y,expo-1))+a+“+”+(Math.pow(y,expo));
返回x;
}
否则如果(世博会==6)
{
x=a+“^”+expo+“+”+(pascal6[1]*y)+a+“^”+(expo-1)+“+”+”+(pascal6[2]*Math.pow(y,expo-4))+a+“^”+(expo-2)+“+”+(pascal6[3]*
Math.pow(y,expo-3))+a+“^”+(expo-3)+“+”+(pascal6[4]*Math.pow(y,expo-2))+a+“^”+(expo-4)+“+”+(pascal6[5]*Math.pow(y,expo-1))+
a+“+”+(Math.pow(y,expo));
返回x;
}
否则如果(世博会==7)
{
x=a+“^”+expo+“+”+(pascal7[1]*y)+a+“^”+(expo-1)+“+”+”+(pascal7[2]*Math.pow(y,expo-5))+a+“^”+(expo-2)+“+”+(pascal7[3]
*Math.pow(y,expo-4))+a+“^”+(expo-3)+“+”+(pascal7[4]*Math.pow(y,expo-3))+a+“^”+(expo-4)+“+”+(pascal7[5]*Math.pow(y,expo-2))
+a+“^”+(expo-5)+(pascal7[6]*Math.pow(y,expo-1))+a+“+”+(Math.pow(y,expo));
返回x;
}
返回x;
}
}

假设a是要从中删除数字的字符串,请尝试此

a = a.replaceAll("[0-9]*$", "");

这将删除所有数字。

我建议使用
字符串的
match()
函数查看
a
中的值是否为有效答案(我不知道什么对您有效)。如果无效,你可以使用<
  public void actionPerformed(ActionEvent event) 
  {
      a = firstVar.getText();
      y = Integer.parseInt(txtSecond.getText());
      expo = Integer.parseInt(exp.getText());

      if(a.matches(/*Some REGEX*/)) {
          outputExpanded.setText(expand());
      }
      else {
          //Use the JDialog
      } 
  }
public static String Binomials(int limit, int y, String variable) {
    StringBuilder result = new StringBuilder("");
    for (int i = 0; i <= limit; i++) {
        if (i>0) {
            result.append("+"+variable );
            if (i>1) {
                result.append("^" + i );
            }
            result.append("*");
        }
        result.append(  Math.pow(y, limit-i) * coefficient(limit, i));
    }
    return (result.toString());
}